Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp repeater button text change

Good day, I want the text of the button inside the repeater to change dynamically based on what the sql selected values would have.

here's my code:

asp.cs

if (!IsPostBack) 
{
   string getEmp = "Select employeeid, photo, lastname, firstname, departmentname, designation,userType from tblEmployee e inner join tblDepartment d on d.departmentid=e.department";
   SqlCommand com = new SqlCommand(getEmp,con);
   con.Open();
   SqlDataReader dr = com.ExecuteReader();
   Button btnSet = (Button)FindControl("btnSet");
   if (dr.HasRows)
   {
      if (dr.Read())
      {
         if (btnSet != null)
         {
             if (dr["userType"].ToString() == "2")
             {
                btnSet.Text = "Set as Normal User";
             }
             else
             {
                btnSet.Text = "Set as Power User";
             }
         }
      } 
   }
   Repeater1.DataSource = dr;
   Repeater1.DataBind();
   dr.Dispose();
   con.Close();

aspx

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<asp:Button ID="btnSet" commandname="set" commandargument=<%# Eval  ("employeeid") %> runat="server" class="tiny success expand button"  Text="" />

like image 595
user3264955 Avatar asked Sep 02 '25 02:09

user3264955


2 Answers

You can subscribe to the Repeater's ItemDatabound event. It allows you to access the controls of the item and change them based on the values of the current item:

private void Repeater1_ItemDatabound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        // Retrieve button of the line
        var btn = e.Item.FindControl("btnSet") as Button;
        if (btn != null)
        {
            // Set text of button based on e.Item.DataItem;
        }
    }
}
like image 59
Markus Avatar answered Sep 05 '25 06:09

Markus


try this its too much short and working

    <asp:Button ID="btnSet" commandname="set" commandargument=<%# Eval("employeeid") %> runat="server" class="tiny success expand button"  Text='<%# Eval("userType").ToString() == "2" ?"Set as Normal User" : "Set as Power User" %>' />

let me know if you need any more help

like image 38
Developerzzz Avatar answered Sep 05 '25 05:09

Developerzzz