开发者

How can I access the button placed in datalist I want to access button text that's bind with the database table field

<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource2" onitemcommand="DataList2_ItemCommand">
   <ItemTemplate&g开发者_开发知识库t;
      <div class="style49">
         <table style="width:100%;">
            <tr>
               <td class="style50">
                  <asp:Image ID="Image3" runat="server" Height="61px" 
                             ImageUrl='<%# Eval("F_Image") %>' Width="48px" />
               </td>
               <td>
                  <asp:LinkButton ID="LinkButton3" runat="server" Height="25px"
                                  TabIndex="1" Text='<%# Eval("Name") %>' 
                                  Width="92px" CommandName="view_frnd">
                  </asp:LinkButton>
               </td>
            </tr>
         </table>
      </div>
   </ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT * FROM [Friends] WHERE ([Email] = @Email)">
    <SelectParameters>
       <asp:SessionParameter Name="Email" SessionField="uid" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
   if (e.CommandName == "view_frnd")
   {
      Response.Write(e.CommandName.ToString());
   }
}

Is succsessfully execute but I want to text of linkbutton that's bind with database table field. How can I access linkbutton3 text?


protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "view_frnd")
    {
        // get the link button in the row selected
        LinkButton LinkButton3 = (LinkButton)e.Item.FindControl("LinkButton3");
        // get its text
        Response.Write(LinkButton3.Text);
    }
}


If what you want is access to the value, then you should be able to bind the same value to the CommandArgument property of your LinkButton, and it will show up as the CommandArgument value in the DataListCommandEventArgs of the DataList's event arguments when the button is clicked.

CommandArgument='<%# Eval("Name") %>'

In your event handler, you would access it as:

protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "view_frnd")
    {
        Response.Write(e.CommandArgument);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜