达人帮忙啊,弄了一天了,人都晕了啊
问题就是,我在一个GridView里用模板列加入了一列DropDownList,然后我又在GridView下面加了一个Button,想通过在点这个按钮的时候,把每行的DropDownList的取值也就是SelectedValue给取出来,不过我不管怎么写都是取不到值的,取到的值都是一开始就赋给DropDownList的值。代码如下, 达人帮忙啊。看看有没有什么问题
这是aspx的代码,里面是一个GridView和一个Button <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField />
<asp:BoundField DataField="EmployeeID" HeaderText="编号" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="BirthDate" DataFormatString="{0:yyyy年M月dd日}" HeaderText="BirthDate" HtmlEncode="False" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0" Selected="True">第一项</asp:ListItem>
<asp:ListItem Value="1">第二项</asp:ListItem>
<asp:ListItem Value="2">第三项</asp:ListItem>
<asp:ListItem Value="3">第四项</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
这里是在后端的Button的响应代码 protected void Button1_Click(object sender, EventArgs e)
{
string strTest = "";
for(int i=0; i<GridView1.Rows.Count; i++)
{
DropDownList ddlst = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
strTest += i.ToString() + ddlst.SelectedIndex.ToString() + "<br>";
}
Response.Write(strTest);
}
对了,这个是做的一个测试的代码,也是取不出来值,在Page_Load里面我没有判断IsPostBack,因为我没有给下拉列表初值。
请求达人相助啊,谢谢了
问题就是,我在一个GridView里用模板列加入了一列DropDownList,然后我又在GridView下面加了一个Button,想通过在点这个按钮的时候,把每行的DropDownList的取值也就是SelectedValue给取出来,不过我不管怎么写都是取不到值的,取到的值都是一开始就赋给DropDownList的值。代码如下, 达人帮忙啊。看看有没有什么问题
这是aspx的代码,里面是一个GridView和一个Button <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField />
<asp:BoundField DataField="EmployeeID" HeaderText="编号" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="BirthDate" DataFormatString="{0:yyyy年M月dd日}" HeaderText="BirthDate" HtmlEncode="False" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0" Selected="True">第一项</asp:ListItem>
<asp:ListItem Value="1">第二项</asp:ListItem>
<asp:ListItem Value="2">第三项</asp:ListItem>
<asp:ListItem Value="3">第四项</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
这里是在后端的Button的响应代码 protected void Button1_Click(object sender, EventArgs e)
{
string strTest = "";
for(int i=0; i<GridView1.Rows.Count; i++)
{
DropDownList ddlst = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
strTest += i.ToString() + ddlst.SelectedIndex.ToString() + "<br>";
}
Response.Write(strTest);
}
对了,这个是做的一个测试的代码,也是取不出来值,在Page_Load里面我没有判断IsPostBack,因为我没有给下拉列表初值。
请求达人相助啊,谢谢了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货