about using the self-contained edit template in the case of dorpdownlist in the GridView

Source: Internet
Author: User

Today, take a look at the method of using the GridView self-contained edit mode in the GridView if there is DropDownList.

Well, today's question is a bit of a detour, explain the purpose in detail.

Because the data of some columns in the GridView is brought out from the Basedata, when editing the GridView, the user wants to manually select the column value instead of the manual input (the system will error if the input is incorrect), the above is the background.

OK, think about, in the GridView can be implemented this function, with the GridView Editor template, data rendering with a label binding, data editing with DropDownList, the following post code:

Front desk:

<asp:gridview id="GridView1"runat="Server"autogeneratecolumns="False"onrowcancelingedit="Gridview1_rowcancelingedit"onrowdeleting="gridview1_rowdeleting"onrowediting="gridview1_rowediting"onrowupdating="gridview1_rowupdating"Onrowdatabound="GridView1_RowDataBound"> <Columns> <asp:templatefield headertext="category"> <ItemTemplate> <asp:label id="Label1"runat="Server"text='<% #Bind ("category")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:label id="Label2"runat="Server"text='<% #Bind ("category")%>'cssclass="Hidecolumn"></asp:Label> <asp:dropdownlist id="drpcategory"runat="Server"Width="156px"> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:commandfield headertext="Edit"showeditbutton="True"/> <asp:commandfield headertext="Delete"showdeletebutton="True"/> </Columns> </asp:GridView>
View Code

Background:

protected voidPage_Load (Objectsender, EventArgs e) {        if(!IsPostBack)        {Bindgridview (); }    }    protected voidGridview1_rowediting (Objectsender, GridViewEditEventArgs e) {Gridview1.editindex=E.neweditindex;    Bindgridview (); }    protected voidGridview1_rowdeleting (Objectsender, Gridviewdeleteeventargs e) {    }    protected voidGridview1_rowupdating (Objectsender, Gridviewupdateeventargs e) {    }    protected voidGridview1_rowcancelingedit (Objectsender, Gridviewcancelediteventargs e) {Gridview1.editindex= -1;    Bindgridview (); }    protected voidGridView1_RowDataBound (Objectsender, GridViewRowEventArgs e) {        if(E.row.rowtype = =datacontrolrowtype.datarow) {intITest =E.row.rowindex; if(Gridview1.editindex = =0) {Label Label2= (Label) E.row.findcontrol ("Label2"); stringStrcategory =Label2.                Text.trim (); DropDownList drpcategory= (DropDownList) E.row.findcontrol ("drpcategory");            Binddropdownlist (drpcategory,strcategory); }        }    }    Private voidBinddropdownlist (DropDownList drpcategory,stringstrcategory) {DataTable dt=NewDataTable (); Dt. Columns.Add ("category"); Drpcategory.datasource=DT;        Drpcategory.databind (); Drpcategory.datatextfield="category"; Drpcategory.datavaluefield="category"; DrpCategory.Items.Insert (0,NewListItem ("Please select","0")); DrpCategory.Items.Insert (1,NewListItem ("test1","test1")); DrpCategory.Items.Insert (2,NewListItem ("test2","test2")); DrpCategory.Items.Insert (3,NewListItem ("Test","Test")); Drpcategory.selectedvalue=strcategory; }    Private voidBindgridview () {DataTable dt=NewDataTable (); Dt. Columns.Add ("category"); //dt. Rows.Add (dt. NewRow ());DataRow row =dt.        NewRow (); row["category"] ="Test"; Dt.        Rows.Add (row);  This. Gridview1.datasource =DT;  This.    Gridview1.databind (); }
View Code

Effect:

Click Edit

Displays DropDownList, and binds the value of the label

OK, finished, sleep ~ Monday continue to work!

about using the self-contained edit template in the case of dorpdownlist in the GridView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.