我在gridview中进行插入/更新和删除。为此我使用ItemTemplate包含标签来显示值。但是当gridview处于编辑模式时,下拉列表代替了这些标签。我想将下拉列表的选定值设置为标签的值。我的下拉列表没有数据源。我绑定下拉列表从0到99.下面是我的编辑方法的代码。
protected void grdUsedCatheters_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
grdUsedCatheters.EditIndex = e.NewEditIndex;
BindCatheterGrid();
DropDownList ddlFrom = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddFrom");
DropDownList ddlTo = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddTo");
BindDropDowns(ddlFrom);
BindDropDowns(ddlTo);
}
catch (Exception ex)
{
if (ex.HelpLink == null)
lblMessage.Text = ex.Message;
else
lblMessage.Text = ex.HelpLink;
lblMessage.CssClass = "ERROR";
}
private void BindDropDowns(DropDownList ddl)
{
for (int i = 0; i <= 99; i++)
ddl.Items.Add(i.ToString());
}
下面的是我的gridview标记的一部分
<asp:TemplateField HeaderText="Cine Run">
<ItemTemplate>
From: <asp:Label ID="lblFrom" runat="server" ><%# Eval("CineRunFrom")%></asp:Label>
To: <asp:Label ID="lblTo" runat="server"><%# Eval("CineRunTo")%></asp:Label>
</ItemTemplate>
<EditItemTemplate>
From: <asp:DropDownList ID="ddFrom" runat="server" Width="50px">
</asp:DropDownList>
To: <asp:DropDownList ID="ddTo" runat="server" Width="50px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
From: <asp:DropDownList ID="ddFromF" runat="server" Width="50px"> </asp:DropDownList>
To: <asp:DropDownList ID="ddToF" runat="server" Width="50px"> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
}
答案 0 :(得分:3)
在设置grdUsedCatheters.EditIndex = e.NewEditIndex
并调用BindCatheterGrid()
方法之前检索label的值,然后在填充DropDownLists后相应地设置其选定值。像这样:
protected void grdUsedCatheters_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
Label lblFrom = (Label)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("lblFrom"); //lblFrom is the ID of label
grdUsedCatheters.EditIndex = e.NewEditIndex;
BindCatheterGrid();
DropDownList ddlFrom = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddFrom");
DropDownList ddlTo = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddTo");
BindDropDowns(ddlFrom);
BindDropDowns(ddlTo);
ddlFrom.Text = lblFrom.Text;
}
catch (Exception ex)
{
if (ex.HelpLink == null)
lblMessage.Text = ex.Message;
else
lblMessage.Text = ex.HelpLink;
lblMessage.CssClass = "ERROR";
}
}
修改强> 并且还改变你的gridview标记:
<asp:TemplateField HeaderText="Cine Run">
<ItemTemplate>
From: <asp:Label ID="lblFrom" runat="server" Text='<%# Eval("CineRunFrom")%>' />
To: <asp:Label ID="lblTo" runat="server" Text='<%# Eval("CineRunTo")%>' />
</ItemTemplate>
...
答案 1 :(得分:1)
我认为这个例子适合你。
首先,您将隐藏字段放在EditItemTemplate中,其中您放置了Dropdownlist。 在ItemTemplate
中设置label的值时设置隐藏字段的值请参阅我的代码:
<asp:GridView runat="server" ID="gridExample" OnRowEditing="gridExample_RowEditing"
AutoGenerateEditButton="True" AutoGenerateColumns ="false" OnRowCancelingEdit ="gridExample_RowCancelingEdit" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblID" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="drpName">
</asp:DropDownList>
<asp:HiddenField runat ="server" ID ="hdnId" Value ='<%# Eval("ID") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblName" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox runat ="server" ID="txtName" Text ='<%# Eval("Name") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gridExample_RowEditing(object sender, GridViewEditEventArgs e)
{
gridExample.EditIndex = e.NewEditIndex;
BindGrid();
DropDownList dl=new DropDownList ();
dl = (DropDownList)gridExample.Rows[gridExample.EditIndex].FindControl("drpName");
FillDrops(dl);
HiddenField hdnId = new HiddenField();
hdnId = (HiddenField)gridExample.Rows[gridExample.EditIndex].FindControl("hdnId");
dl.Text = hdnId.Value;
}