GridView中 LinkButton兩種方式


<asp:TemplateField HeaderText="操作" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbtnPhoneHide" runat="server"  CommandArgument='<%# Eval("id") %>'
CommandName="1" Text="操作"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

 

一、

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

try//因為gridview一行中既有Button也有LinkButton,而下面獲取gvrow需要用到,所以用try,要不然點擊Button時會報錯無法轉換
{

GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);//獲取被點擊的linkButton所在的GridViewRow
int i = gvrow.RowIndex;//獲取到行索引 RowIndex

if ("1" == e.CommandName)
{

long longPhoneId = Convert.ToInt64(e.CommandArgument);
Label lbPhone = gvXyPhone.Rows[i].FindControl("lbPhoneG") as Label;

}

}
catch (Exception)
{
}

}

 

二、

 在 linkbutton中增加 OnClick="lbtnPhoneHide_Click" 事件

protected void lbtnPhoneHide_Click(object sender, EventArgs e)
{
long longPhoneId = Convert.ToInt64(((LinkButton)sender).CommandArgument);

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM