加载时如何在gridview中绑定下拉值 [英] how to bind dropdown values in gridview while loading
本文介绍了加载时如何在gridview中绑定下拉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用gridview,在内部我正在使用下拉菜单.
当我单击gridview中的Edit链接时,那么我想显示具有现有存储值的下拉列表.
但是,我在下拉列表中获取存储的值.它始终显示选择".
有什么问题..
ASPX代码
----------
I am using gridview and inside i am using dropdown.
When i click the Edit link in gridview, then i want to display the dropdown with existing stored values.
But, i am getting stored values in the dropdown. it is always displaying "select".
what is the problem..
ASPX Code
----------
<asp:TemplateField HeaderText="Remarks Category" HeaderStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:DropDownList ID="drpstatus" runat="server" Style="width: 100px;">
<asp:ListItem Text="Select" Value=""></asp:ListItem>
<asp:ListItem >Efficiency</asp:ListItem>
<asp:ListItem>Estimation</asp:ListItem>
<asp:ListItem>Internal Changes</asp:ListItem>
<asp:ListItem>External Changes</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text=<%#Eval("EmpName")%></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
ASPX.Vb代码
--------------
ASPX.Vb Code
--------------
If e.Row.RowType = DataControlRowType.DataRow Then
Dim drpstatus As DropDownList = DirectCast(e.Row.FindControl("drpstatus"), DropDownList)
If drpstatus IsNot Nothing Then
drpstatus.SelectedIndex = drpstatus.Items.IndexOf(drpstatus.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, EmpName
End If
End If
推荐答案
尝试此操作,首先获取一个模板字段,然后添加项目模板,然后在编辑模板内部将下拉列表如下...
try this ,first take a template field then add item template,then inside edit template put the dropdown as follow...
<asp:templatefield headertext="Status" sortexpression="with_status" xmlns:asp="#unknown">
<itemtemplate>
<asp:label id="Label1" runat="server" text="<%# Bind("with_status") %>"></asp:label>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem value="0">-Select- </asp:listitem>
<asp:listitem value="1">pending </asp:listitem>
<asp:listitem value="2">completed </asp:listitem>
</asp:dropdownlist>
</edititemtemplate>
</asp:templatefield>
aspx.cs
在rowupdating上输入以下代码
aspx.cs
on rowupdating put the following code
DropDownList ddl2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
if (ddl2.SelectedValue.ToString() != "0")
{
SqlDataSource1.UpdateParameters["with_status"].DefaultValue = ddl2.SelectedItem.ToString();
}
这篇关于加载时如何在gridview中绑定下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文