<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="姓名" />
<asp:TemplateField HeaderText="班次">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="获取" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>当我点击 ”获取“按钮时 怎么获取 GridView 当前行 DropDownList 控件的值
<Columns>
<asp:BoundField HeaderText="姓名" />
<asp:TemplateField HeaderText="班次">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="获取" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>当我点击 ”获取“按钮时 怎么获取 GridView 当前行 DropDownList 控件的值
先将CommandName = "GetValue"
然后再在GridView里面添加一个RowCommand 事件..
在里面写一个判断语句
if(e.CommandName == "GetValue")
{
//处理语句
}这样就获取到DropDownList里面的值了...
怎么获取当 当前行 DropDownList 控件值
先设置Button2的CommandName,CommandArgument
DropDownList ddl = (DropDownList)GridView1.Rows[e.CommandArgument].FindControl("DropDownList1");
.....
DropDownList ddl = (DropDownList)GridView1.Rows[e.CommandArgument].FindControl("DropDownList1");
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddl" runat="server" Width="100px">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="select" />
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex;
Button btn = (Button)e.Row.FindControl("Button1");
btn.CommandArgument = id.ToString();
}
} protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "select")
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
DropDownList ddl=GridView1.Rows[rowIndex].Cells[6].FindControl("ddl") as DropDownList;
string ddlValue = ddl.SelectedItem.Text.Trim();
}
}