如何在GridView ItemTemplate中找到控件 [英] How to find control in gridview itemtemplate

查看:131
本文介绍了如何在GridView ItemTemplate中找到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想找到对itemtemplate中的linkbutton的控制,我使用了以下代码,并获取了值,但是由于使用了for循环,我仅获取了最后一个值,而先前的值也被清除了,这是我的代码pl告诉我如何获取正确的值

Hi

I want to find control of a linkbutton which is in the itemtemplate and I used the following code and i get the value but because of using the for loop I am getting only the last value and the previous values is getting cleared here is my code pl tell me how to get the correct value

<asp:TemplateField HeaderText="Associate ID">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkAssId" Text='<%# Eval("associate_id") %>' runat="server" 
                                    onclick="lnkAssId_Click">LinkButton</asp:LinkButton>
                            </ItemTemplate>
</asp:TemplateField>


for (int i = 0; i < grdApproval.Rows.Count; i++)
        {
            LinkButton lblAssId = (LinkButton)grdApproval.Rows[i].FindControl("lnkAssId");
            Session["AssId"] = lblAssId.Text;
        }

推荐答案

获得最后一个值的原因是因为您要更新/覆盖循环中的lblAssId.Text.

您可以通过将每个值添加到列表并显示列表中的值来解决此问题.或将最后一个标签值添加到现有会话中.



The reason that you are getting the last value because you are updating/overwriting the lblAssId.Text in your loop.

You can solve this issue either by adding each value to a list and display the values from the list. Or add the last label value to the existing Session.

Like

Session["AssId"] = Session["AssId"].ToString() + " " + lblAssId.Text;




Or

List<string> AssIds = new List<string>();
for (int i = 0; i < grdApproval.Rows.Count; i++)
{
  LinkButton lblAssId = (LinkButton)grdApproval.Rows[i].FindControl("lnkAssId");
  AssIds.Add(lblAssId.Text);
}
Sesion["AssId"] = AssIds;



然后使用foreach或for循环将列表中的所有值写入.

祝你好运,
OI



Then write all the values in the list with a foreach or for loop.

Good luck,
OI


这篇关于如何在GridView ItemTemplate中找到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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