2012-03-24 84 views
2

我有一個網格視圖,其中包含模板字段中的按鈕。我想在按鈕點擊事件完成時更改按鈕文本。有人可以給我一個示例代碼。更改gridview的按鈕文本

預先感謝

+0

我們能看到什麼你都試過,你遇到什麼錯誤? – Seany84 2012-03-24 12:17:17

+0

我的回答對你有幫助嗎? – Seany84 2012-03-25 01:28:02

回答

3

下面是代碼使用RowCommand()在GridView的採樣位。

ASPX

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Label ID="lbl1" runat="server"></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField ShowHeader="False"> 
      <ItemTemplate> 
       <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="MYCOMMAND" Text="My Text!" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

C#

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     List<string> lst = new List<string>() { "asd", "xxx" }; 
     GridView1.DataSource = lst; 
     GridView1.DataBind(); 
    } 
} 

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "MYCOMMAND") 
    { 
     Button Button1 = (Button)e.CommandSource; 
     if (Button1 != null) 
      Button1.Text = "changed text.."; 
    } 
} 
+0

這是我的網格視圖: – arun 2012-03-26 05:49:24

+0

我不明白你的意思?你有沒有嘗試我上面發佈的代碼? – Seany84 2012-03-26 09:17:07