Question

I want button in the datalist OnClick gets text from both textboxes on the same row of the button.. how can i refer to that using C# keeping in mind that I want to use my own stored procedures and functions "OnClicking" buttons without using SqlDataSource Control

<asp:DataList ID="DataList1" runat="server">
  <ItemTemplate>
    <table class="auto-style2">
      <tr>
        <td>
          <asp:Label ID="Label1" runat="server" Text='<%# Eval("UDI") %>'></asp:Label>
        </td>
        <td>
          <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
        </td>
        <td>
          <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("describtion") %>'></asp:TextBox>
        </td>
        <td>
          <asp:Button ID="Button2" runat="server" Text="Modify" CommandArgument='<%# Eval("UDI") %>' OnCommand="Button2_Command" />
        </td>
      </tr>
    </table>
  </ItemTemplate>
</asp:DataList>

and here is the code behind

AdminControlEntities db = new AdminControlEntities();
var x=db.sp_GetAllProducts(); //Stored procedure that returns a selection of data
DataList1.DataSource = x.ToList();
DataList1.DataBind();

till here i get my data viewed correctly, I need to Update using my own stored procedure in this example from TextBox1 and TextBox2 to the Label1 ID

Was it helpful?

Solution

ADD OnItemCommand to your mark up

<asp:DataList ID="DataList1" runat="server" OnItemCommand="Modify_ItemCommand" >

Then on Code Behind:

protected void Modify_ItemCommand(object source, DataListCommandEventArgs e)
    {
 
        /* select the row index */
        int index = Convert.ToInt32(e.Item.ItemIndex);
 
        /*To get and Textbox of selected row*/
        TextBox txtbx = (TextBox)e.Item.FindControl("TextBox1");
 
        /* Assigning Value to your textbox */
        txtbx.Text = "What ever you want here";
 
    }

OTHER TIPS

You can declare the button using the property CommandName and then use the ItemCommand event controller.

Source: http://msdn.microsoft.com/es-es/library/es4e4e0e(v=vs.100).aspx

Here, a working example (Note that I'm using an UpdatePanel. Due the content of the page is going to be change, if you do not use it you will get a server error. More info here):

Test.aspx

<asp:UpdatePanel ID="upDataList1" runat="server" ChildrenAsTriggers="true">
  <ContentTemplate>       
    <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
      <ItemTemplate>
        <table>
          <tr>
            <td>
              <asp:Label ID="lUID" runat="server" Text='<%# Eval("UID") %>' />
            </td>
            <td>
              <asp:TextBox ID="tbName" runat="server" Text='<%# Eval("name") %>' />
            </td>
            <td>
              <asp:TextBox ID="tbDescription" runat="server" Text='<%# Eval("description") %>' />
            </td>
            <td>
              <asp:Button ID="bModify" runat="server" Text="Modify" CommandName="Modify" />
            </td>
          </tr>
        </table>
      </ItemTemplate>
    </asp:DataList>
  </ContentTemplate>
</asp:UpdatePanel>

Test.aspx.cs

public partial class Test : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
    if (!Page.IsPostBack) {
      var x = db.sp_GetAllProducts();
      DataList1.DataSource = x;
      DataList1.DataBind();
    }
  }
  protected void DataList1_ItemCommand(Object sender, DataListCommandEventArgs e) {
    String a = ((TextBox) e.Item.FindControl("tbName")).Text;
    String b = ((TextBox) e.Item.FindControl("tbDescription")).Text;
    ((Label) e.Item.FindControl("lUID")).Text = a + " " + b;
  }
}

public class db {
  public String UID { get; set; }
  public String name { get; set; }
  public String description { get; set; }

  public db(String UID, String name, String description) {
    this.UID = UID;
    this.name = name;
    this.description = description;
  }
  public static List<db> sp_GetAllProducts() {
    List<db> list = new List<db>();
    list.Add(new db("1", "1a", "1b"));
    list.Add(new db("2", "2a", "2b"));
    list.Add(new db("3", "3a", "3b"));
    list.Add(new db("4", "4a", "4b"));
    list.Add(new db("5", "5a", "5b"));
    list.Add(new db("6", "6a", "6b"));
    return list;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top