如何获得在这种情况下一个GridView单元格值 [英] How to get value of a gridview cell in this scenario

查看:126
本文介绍了如何获得在这种情况下一个GridView单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView里面的链接按钮,如下:

 < ASP:的TemplateField的HeaderText =分析>
                                 <&ItemTemplate中GT;
                                   < ASP:LinkBut​​ton的ID =LinkBut​​ton1文本=分析=服务器的OnClick =LinkBut​​ton1_Click/>
                                 < / ItemTemplate中>
                             < / ASP:的TemplateField>

我有LinkBut​​ton1_Click功能如下:

 保护无效LinkBut​​ton1_Click(对象发件人,EventArgs的发送)
    {        testtb.Text =名称;
        Console.WriteLine(名);
     }

此变量name是GridView.I的第一列正在获得用于名称的值如下:

 保护无效UnanalysedGV_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        如果(e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView的D =(DataRowView的)e.Row.DataItem;
            名称= D [resultId]的ToString()。        }    }

我要那个链接点击按钮的GridView的该行第一列的值将成为文本框testtb的文本。

不知何故,名称值保持为空。

修改

我发现可能的RowDataBound不适合我的要求正确的事件,因为我需要为每个行的值。

所以我删除了的RowDataBound功能。

我想我要处理这里面LinkBut​​ton1_Click本身。

我添加了此行的功能:

  NAME = UnanalysedGV.SelectedRow.Cells [1]。文本;

还不行。
没有人有任何想法?


解决方案

您可以用两种方法做如下所述。


  

通过事件的LinkBut​​ton的手柄click事件冒泡code -


 < ASP:GridView控件的AutoGenerateColumns =假=服务器ID =grdCustomPaggingOnRowCommand =grdCustomPagging_RowCommand>
   <柱体和GT;
       < ASP:BoundField的数据字段=ROWNUMBER的HeaderText =ROWNUMBER/>
       < ASP:BoundField的数据字段=DealId的HeaderText =DealID/>
       < ASP:BoundField的数据字段=Dealtitle的HeaderText =DealTitle/>
       < ASP:的TemplateField的HeaderText =查看>
        <&ItemTemplate中GT;
       < ASP:LinkBut​​ton的=服务器ID =lnkViewCommandArgument ='<%#的eval(DealId)%>'
         的CommandName =查看>查看新政< / ASP:LinkBut​​ton的>
         < / ItemTemplate中>
       < / ASP:的TemplateField>
   < /专栏>
< / ASP:GridView的>保护无效grdCustomPagging_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    如果(e.CommandName ==VIEW)
    {
        LinkBut​​ton的lnkView =(LinkBut​​ton的)e.CommandSource;
        字符串dealId = lnkView.CommandArgument;
    }
}


  

LinkBut​​ton的句柄click事件直接点击事件code -


 < ASP:GridView控件的AutoGenerateColumns =假=服务器ID =grdCustomPagging>
   <柱体和GT;
       < ASP:BoundField的数据字段=ROWNUMBER的HeaderText =ROWNUMBER/>
       < ASP:BoundField的数据字段=DealId的HeaderText =DealID/>
       < ASP:BoundField的数据字段=Dealtitle的HeaderText =DealTitle/>
       < ASP:的TemplateField的HeaderText =查看>
        <&ItemTemplate中GT;
       < ASP:LinkBut​​ton的=服务器ID =lnkView的OnClick =lnkView_Click>查看新政< / ASP:LinkBut​​ton的>
         < / ItemTemplate中>
       < / ASP:的TemplateField>
   < /专栏>
< / ASP:GridView的>保护无效lnkView_Click(对象发件人,EventArgs的发送)
{
    GridViewRow grdrow =(GridViewRow)((LinkBut​​ton的)寄件人).NamingContainer;
    字符串ROWNUMBER = grdrow.Cells [0]。文本;
    字符串dealId = grdrow.Cells [1]。文本;
    串dealTitle = grdrow.Cells [2]。文本;
}

希望这有助于。

I have a link button inside a gridView as below :

 <asp:TemplateField HeaderText="Analyze">
                                 <ItemTemplate>
                                   <asp:LinkButton ID="LinkButton1" Text="Analyze"  runat="server"  OnClick="LinkButton1_Click"  />                                 
                                 </ItemTemplate>
                             </asp:TemplateField>

I have the LinkButton1_Click function as below :

 protected void LinkButton1_Click(object sender, EventArgs e)
    {     

        testtb.Text = name;
        Console.WriteLine(name);
     }

This variable "name" is the first column of the GridView.I am obtaining the value for "name" as below:

protected void UnanalysedGV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView d = (DataRowView)e.Row.DataItem;
            name = d["resultId"].ToString();

        }

    }

I want that on click of the link button the value of first column in that row of gridview becomes the text for the textbox testtb.

Somehow,the value for name remains null.

EDIT

I found out that probably RowDataBound isn't the correct event for my requirements because I need the value for each row.

So I removed the RowDataBound function.

I guess I have to handle this inside LinkButton1_Click itself.

I added this line to the function :

name = UnanalysedGV.SelectedRow.Cells[1].Text;

Still doesn't work. Does anyone have any idea ?

解决方案

You can do it using two approaches as mentioned below.

Handle click event of linkbutton through event bubbling code -

<asp:GridView AutoGenerateColumns="false" runat="server" ID="grdCustomPagging" OnRowCommand="grdCustomPagging_RowCommand">
   <Columns>
       <asp:BoundField DataField="RowNumber" HeaderText="RowNumber" />
       <asp:BoundField DataField="DealId" HeaderText="DealID" />
       <asp:BoundField DataField="Dealtitle" HeaderText="DealTitle" />
       <asp:TemplateField HeaderText="View">
        <ItemTemplate>
       <asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("DealId") %>'
         CommandName="VIEW">View Deal</asp:LinkButton>
         </ItemTemplate>
       </asp:TemplateField>
   </Columns>
</asp:GridView>

protected void grdCustomPagging_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "VIEW")
    {
        LinkButton lnkView = (LinkButton)e.CommandSource;
        string dealId = lnkView.CommandArgument;
    }
}

Handle click event of linkbutton directly click event code -

<asp:GridView AutoGenerateColumns="false" runat="server" ID="grdCustomPagging">
   <Columns>
       <asp:BoundField DataField="RowNumber" HeaderText="RowNumber" />
       <asp:BoundField DataField="DealId" HeaderText="DealID" />
       <asp:BoundField DataField="Dealtitle" HeaderText="DealTitle" />
       <asp:TemplateField HeaderText="View">
        <ItemTemplate>
       <asp:LinkButton runat="server" ID="lnkView" OnClick="lnkView_Click">View Deal</asp:LinkButton>
         </ItemTemplate>
       </asp:TemplateField>
   </Columns>
</asp:GridView>

protected void lnkView_Click(object sender, EventArgs e)
{
    GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
    string rowNumber = grdrow.Cells[0].Text;
    string dealId = grdrow.Cells[1].Text;
    string dealTitle = grdrow.Cells[2].Text;
}

Hope this helps.

这篇关于如何获得在这种情况下一个GridView单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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