我有一個包含30列以上的GridView。大多數是普通的控件,但對於一些我添加了模板控件(DropDownList,Calendar和CheckBox控件)。這裏是有問題的控件的aspx代碼在Asp.Net中單擊GridView上的更新時獲取下拉列表的實例
<asp:TemplateField HeaderText="Field1 Caption" SortExpression="Field1">
<ItemTemplate>
<asp:Label ID="lblConstructionArea" runat="server" Text='<%# Eval("Field1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlField1" EnableViewState="true" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
我希望在用戶點擊編輯時在列上顯示下拉列表。因此,我添加此代碼(和上述EditItemTemplate中)
protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
{
string fieldOne = CommonUtils.ExtractControlValue(e,"lblField1",gvData);
gvData.SelectedIndex = e.NewEditIndex;
gvData.EditIndex = e.NewEditIndex;
gvData.DataBind();
BindGridDropDownData(e, CommonUtils.GetConstructionAreas() ,"ddlConstructionArea", constructionArea, "Field1", fieldOne);
}
在上面的代碼我正在當前可用並將它傳遞給另一種方法,以便顯示下拉時,能夠準確地示出的選擇的索引。這之後,我做了下拉列表的變化上,並點擊GridView的「更新按鈕」和下面的事件被觸發
protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowEditIndex = e.RowIndex;
GridViewRow gRow = gvData.Rows[rowEditIndex];
DropDownList ddlConstructionArea = (DropDownList) gvData.Rows[rowEditIndex].FindControl("ddlConstructionArea"); //This does not work
ddlConstructionArea = (DropDownList)gRow.FindControl("ddlConstructionArea");//This does not work
ddlConstructionArea = (DropDownList)gvData.Rows[rowEditIndex].Cells[7].FindControl("ddlConstructionArea");//this does not work either
gvData.EditIndex = -1;//this works and the text boxes disappear
gvData.DataBind();//this works and the old data shows up on the gridview
}
我很好奇,如何做一個網格,我有一個更新綁定是運行時。
不是真的有病,那是類型。問題解決了。我會更新這個帖子的答案。再次感謝你的回覆。欣賞它。 – vikramjb