无法获取gridview中linkbutton的文本值
Can't get the text value of linkbutton in gridview
这是我的标记:-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="245px" onrowcommand="GridView1_RowCommand" >
<Columns>
<asp:TemplateField>
<ItemStyle BackColor="#CCCCCC" ForeColor="Black" Width="250px" HorizontalAlign="Center"
BorderStyle="None" />
<ItemTemplate>
<asp:LinkButton ID="userList" runat="server" CommandName="Select" CommandArgument ='<%# Container.DataItemIndex %>' Text='<%# Bind("users") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
并且在代码隐藏中,我试图获取当前行的文本值,但似乎无法获取;
它returns“”。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowValue = Convert.ToInt32(e.CommandArgument.ToString());
GridView1.SelectedIndex = rowValue;
string test = GridView1.SelectedRow.Cells[0].Text;
}
您需要找到并访问控件
LinkButton btn = (LinkButton)gvDealerSupportMail.Rows[rowValue].FindControl("userList");
string result = btn.Text;
您可以使用 CommandSource
获得 Command.Text
、CommandName
和 CommandArgument
。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton commandSource = e.CommandSource as LinkButton;
string commandText = commandSource.Text;
string commandName = commandSource.CommandName;
int commandArgument = Convert.ToInt32(commandSource.CommandArgument);
}
这是我的标记:-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="245px" onrowcommand="GridView1_RowCommand" >
<Columns>
<asp:TemplateField>
<ItemStyle BackColor="#CCCCCC" ForeColor="Black" Width="250px" HorizontalAlign="Center"
BorderStyle="None" />
<ItemTemplate>
<asp:LinkButton ID="userList" runat="server" CommandName="Select" CommandArgument ='<%# Container.DataItemIndex %>' Text='<%# Bind("users") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
并且在代码隐藏中,我试图获取当前行的文本值,但似乎无法获取;
它returns“”。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowValue = Convert.ToInt32(e.CommandArgument.ToString());
GridView1.SelectedIndex = rowValue;
string test = GridView1.SelectedRow.Cells[0].Text;
}
您需要找到并访问控件
LinkButton btn = (LinkButton)gvDealerSupportMail.Rows[rowValue].FindControl("userList");
string result = btn.Text;
您可以使用 CommandSource
获得 Command.Text
、CommandName
和 CommandArgument
。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton commandSource = e.CommandSource as LinkButton;
string commandText = commandSource.Text;
string commandName = commandSource.CommandName;
int commandArgument = Convert.ToInt32(commandSource.CommandArgument);
}