我试图将下拉列表绑定到数据集但我在DDL上获取空值。当我单击一行进行编辑模式时,DDL不会被绑定,因为我无法找到它。为什么我不会找到行下拉列表并绑定它们?
using (var scTyche = new SqlConnection(ConfigurationManager.ConnectionStrings["KondorConnectionConnectionString"].ConnectionString))
{
foreach (GridViewRow row in GridView1.Rows)
{
var ddl = (DropDownList)row.FindControl("DropDownListFolders1");
var da = new SqlDataAdapter();
var dt = new DataTable();
var ds = new DataSet();
scTyche.Open();
var cmdTyche = scTyche.CreateCommand();
cmdTyche.CommandType = CommandType.StoredProcedure;
cmdTyche.CommandText = "dbo.spGetFolders";
cmdTyche.CommandTimeout = 60;
cmdTyche.Parameters.Add("@nBranchId", SqlDbType.Int).Value = intBranchId;
da.SelectCommand = cmdTyche;
da.Fill(dt);
ds.Tables.Add(dt);
ddl.DataSource = ds;
ddl.DataTextField = "strShort";
ddl.DataValueField = "nId";
ddl.DataBind();
cmdTyche.Parameters.Clear();
scTyche.Dispose();
scTyche.Close();
}
}
我的aspx:
<asp:UpdatePanel runat="server" ID="update">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing"
AllowSorting="True" AutoGenerateColumns="False" SkinID="gridviewGridlinesSkin"
OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting"
EmptyDataText="No positions found">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("strPositionId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Folder" SortExpression="strFolderName">
<EditItemTemplate>
<BrummerComp:SortableDropDownList ID="DropDownListFolders1" Width="141px" runat="server"
SkinID="BCdropdownlistSkin"/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
答案 0 :(得分:1)
如果你想从后面的代码中手动绑定下拉列表,我建议你在GridView.OnDataBinding,GridView.OnDataBoung,GridView.OnRowDataBound事件或DropDown.OnDataBinding,DropDown.OnDataBound数据绑定事件中执行此操作。 / p>
现在,您可能在错误的时间尝试数据绑定 - 也许在将行更改为编辑模式之前?
另外请记住,只有1行处于编辑模式 - 如果使用GridView.OnRowDataBound,则可以使用事件args(e.Row.RowState == DataControlRowState.Edit)来确定当前事件是否处理了一行编辑模式与否。这将减少代码中的开销并使其更容易调试(可能会隔离问题)。