我有GridView的列看起来像这样:
<Columns>
<asp:TemplateField HeaderText="Opcje">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Accept" ID="AcceptBtn" CommandName="Accept"/>
<asp:LinkButton runat="server" Text="Deny" ID="DenyBtn" CommandName="Deny"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
在创建新行时,我想更改LinkButton的CommandArgument属性:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
}
问题是,代码似乎没有改变任何东西,当我点击AcceptBtn时,调用下面的代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Accept")
{
string ss = (string)e.CommandArgument;
...
}
}
ss =“”。为什么?如果页面是PostedBack,那么两个CommandArguments都被清除了?
答案 0 :(得分:2)
尝试在CommandArgument
事件中设置RowDataBound
而不是RowCreated
。
答案 1 :(得分:1)
你需要使用像..的
这样的rowdatabound事件 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
}
}
答案 2 :(得分:0)
你在哪里设置
myFiles[fileIndex].Name;
及其每个组件?