gridview asp.net中的CommandArgument [英] CommandArgument in gridview asp.net

查看:130
本文介绍了gridview asp.net中的CommandArgument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 gridview 中使用 linkbutton 并使用 rowcommand property。

但其命令参数显示no。行但我想从我的数据库中输入一些条目(id值)

这里是我的代码:



Html代码:

I am using linkbutton in gridview and handling it with rowcommand property.
but its commandargument shows the no. of row but I want some entry from my database(id value)
here is my code:

Html code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        DataSourceID="SqlDataSource1" AllowPaging="True" 
                        DataKeyNames="Mail_id" onrowcommand="GridView1_RowCommand">
                        <columns>
                          <asp:ButtonField DataTextField="subject" HeaderText="Subject" 
                                SortExpression="subject" Text="Subject" />
                            <asp:ButtonField DataTextField="body" HeaderText="Content" Text="body" />
                            <asp:ButtonField DataTextField="sender" HeaderText="sender" Text="sender" />
                            <asp:ButtonField DataTextField="date" HeaderText="Date" Text="date" />
                        </columns>







c#code:




c# code:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        report.Text = e.CommandArgument.ToString();
    }







请提出建议....




please suggest something....

推荐答案

您可以使用LinkBut​​ton绑定命令参数并传递给RowCommand事件。假设你有以下模板字段。

You can Bind the command argument with the LinkButton and pass into RowCommand Event. Let say you have below Template Field.
<asp:templatefield xmlns:asp="#unknown">
           <headertemplate>
             Status
           </headertemplate>
           <itemtemplate>
               <asp:imagebutton commandname="DataCommand" commandargument="<%# Eval("ID" )%>" text="<%# Eval("WebSite" )%>" imageurl="/test.pmg">
                     ID="lnk" runat="server" />
           </asp:imagebutton></itemtemplate>
       </asp:templatefield>



这里是您需要编写的代码,以便点击图像按钮,




And here is the code you need to write in order to get the image button click,

Protected Void Grid_RowCommand(object sender, GridViewEventArgs e)
{
 if(e.CommandName=="DataCommand") 
    {
      // Get the value of command argument
        var value= e.CommandArgument;
      // Do whatever operation you want.  
    }
}





希望这会有所帮助。



Hope this will help.


finally我找到了解决方案,如果有人有更好的解决方案,请发送。



我们可以使用linkbutton的Click事件从gridview内部获取行信息



例如:



protected void LinkBut​​ton1_Click(object sender,EventArgs e)

{

LinkBut​​ton btn =(LinkBut​​ton)发送者;

GridViewRow行=(GridViewRow)btn.NamingContainer;

Response.Write( 链接的行索引按钮:+ row.RowIndex +

DataKey值:+ GridView1.DataKeys [row.RowIndex] .Value.ToString());

}
finally i found the solution, If someone has a better one, please send it.

we can use the Click event of the linkbutton to get the row information from inside the gridview

For example:

protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
Response.Write("Row Index of Link button: " + row.RowIndex +
"DataKey value:" + GridView1.DataKeys[row.RowIndex].Value.ToString());
}


这篇关于gridview asp.net中的CommandArgument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆