如何绑定gridview中的下拉列表数据 [英] How to bind dropdownlist data from gridview
问题描述
我的数据库表员工喜欢
UserID用户名UserExperience
101 Pandu 1-2
102 Nageswar 2-5
UserExperience专栏
1 - 年
2 - 月
i想要更新101记录
i我在编辑模式下单击编辑按钮101员工详细信息并绑定这些数据(一个下拉列表绑定一个值,第二个下拉列表绑定第二个来自UserExperience的价值(1-2))
my databse table Employee like
UserID UserName UserExperience
101 Pandu 1-2
102 Nageswar 2-5
UserExperience Column
1--years
2--Months
i want update 101 record
i am click Edit button 101 employee details in edit mode and bind those data(one dropdown is bind one value and second dropdown is bind second value from UserExperience(1-2))
<asp:TemplateField HeaderText="Experiance">
<ItemTemplate>
<asp:Label ID="lblExpyear" runat="server" Text='<%#Eval("Experiance")%>' />
</ItemTemplate>
<EditItemTemplate>
<%-- <asp:TextBox ID="txtExp" runat="server" Text='<%#Eval("Experiance")%>' />--%>
<asp:DropDownList ID="ddlExpYear" runat="server">
<asp:ListItem>- Select -</asp:ListItem>
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlExpMonth" runat="server" >
<asp:ListItem>- Select -</asp:ListItem>
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
< b>我有三个编辑:
< asp:TemplateField HeaderText =实验>
< itemtemplate>
< asp:Label ID =lblExpyearrunat =serverText ='<%#Eval(Experiance)%>'/>
< edititemtemplate>
<% - < asp:TextBox ID =txtExprunat =serverText ='<%#Eval(Experiance) %>'/> - %>
< asp:DropDownList ID =ddlExpYearrunat =server>
< asp:ListItem> ; - 选择 -
< asp:ListItem> 0
< asp:ListItem> 1
< asp:ListItem> 2
< asp:ListItem> 3
< asp:ListItem> 4
< asp:ListItem> 5
< asp:ListItem> 6
< asp:ListItem> ; 7
< asp:ListItem> 8
< asp:ListItem> 9
< asp:ListItem> 10 >
< asp:DropDownList ID =ddlExpMonthrunat =server>
< asp:ListItem> - 选择 -
< asp:ListItem> 0
< asp:ListItem> 1
< asp:ListItem> 2
< asp:ListItem> 3
&l t; asp:ListItem> 4
< asp:ListItem> 5
< asp:ListItem> 6
< asp:ListItem> ; 7
< asp:ListItem> 8
< asp:ListItem> 9
< asp:ListItem> 10 >
What I have tried:
<asp:TemplateField HeaderText="Experiance">
<itemtemplate>
<asp:Label ID="lblExpyear" runat="server" Text='<%#Eval("Experiance")%>' />
<edititemtemplate>
<%-- <asp:TextBox ID="txtExp" runat="server" Text='<%#Eval("Experiance")%>' />--%>
<asp:DropDownList ID="ddlExpYear" runat="server">
<asp:ListItem>- Select -
<asp:ListItem>0
<asp:ListItem>1
<asp:ListItem>2
<asp:ListItem>3
<asp:ListItem>4
<asp:ListItem>5
<asp:ListItem>6
<asp:ListItem>7
<asp:ListItem>8
<asp:ListItem>9
<asp:ListItem>10
<asp:DropDownList ID="ddlExpMonth" runat="server" >
<asp:ListItem>- Select -
<asp:ListItem>0
<asp:ListItem>1
<asp:ListItem>2
<asp:ListItem>3
<asp:ListItem>4
<asp:ListItem>5
<asp:ListItem>6
<asp:ListItem>7
<asp:ListItem>8
<asp:ListItem>9
<asp:ListItem>10
推荐答案
试试这个,
创建一个方法代码背后
Try this,
Create a method in the code behind
public string GetYearMonth(string value, string flag)
{
var items = value.Split('-');
if (items.Length == 2)
{
if (flag =="y")
return items[0].Trim();
else
return items[1].Trim();
}
return "- Select -";
}
ASPX:
ASPX:
<asp:DropDownList ID="ddlExpYear" SelectedValue='<%# GetYearMonth(Eval("Experiance").ToString(),"y")%>' runat="server">
.
.
<asp:DropDownList ID="ddlExpMonth" SelectedValue='<%# GetYearMonth(Eval("Experiance").ToString(),"m")%>' runat="server">
.
.
.
这篇关于如何绑定gridview中的下拉列表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!