Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to change the values of label in listview from codebehind?

Status
Not open for further replies.

fsh290

Programmer
Dec 7, 2011
6
Actually i'm developing template using asp.net and c#.
i'm using listview at my ascx page and my ItemTemplate is as below:
Code:
<ItemTemplate>
<tr style="background-color:#FFF8DC;color: #000000;">
    <td>
        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this Product Details?');" />
        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" CausesValidation="True" />
    </td>
    <td>
        <asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' />
    </td>
    <td>
        <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
    </td>
    <td>
        <asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' />
    </td>
    <td>
        <asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
    </td>
    <td>
        <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
    </td>
</tr>
</ItemTemplate>
and i retrieve the data from the database in ascx code behind as bellow:
Code:
public DataTable GetEmployee(string query)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    SqlDataAdapter ada = new SqlDataAdapter(query, con);
    DataTable dtEmp = new DataTable();
    ada.Fill(dtEmp);
    return dtEmp;
}
and also i bind the data in ascx code behind as follow:
Code:
private void BindLVP(string SortExpression)
{
    string UpdateQuery = "Select * from Employee" + SortExpression;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    hid_UpdateQTP.Value = UpdateQuery;

    lvProduct.Items.Clear();
    lvProduct.DataSource = GetEmployee(UpdateQuery);
    lvProduct.DataBind();
}
my question is how i can delete the <%# Eval("EmpID") %> and all the other label text like this in ItemTemplate and change the label.text in ItemTemplate from the code behind, i mean pass the data of the database to these label from code behind.
appreciate your consideration.
 
If i understand you scenario you would like to have the edit and delete buttons wired in the code behind.
Code:
<asp:Button ID="DeleteButton" runat="server" OnCommand="DoWork" CommandAgrument='<%#Eval("EmpId")%>' CommandName="Delete" Text="Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this Product Details?');" />
        <asp:Button ID="EditButton" runat="server" OnCommand="DoWork" CommandAgrument='<%#Eval("EmpId")%>' CommandName="Edit" Text="Edit" CausesValidation="[b]false[/b]" />
Code:
protected void DoWork(object sender, CommandEventArgs e)
{
   switch(e.CommandName)
   {
      case "Delete":
         var employeeId = e.CommandArgument;
         //remove employee from database
         break;
      case "Edit":
         var employeeId = e.CommandArgument;
         //allow user to edit the record
         break;
      default:
          var message = string.Format("'{0}' is an unkown command", e.CommandName);
          throw new InvalidOperationException(message);
   }
   //reload the data
}
note that the edit button does not cause validation. you only need to validate when you insert or update a record.

as for changing all the labels depending on a specific condition.
I would create 2 additional user controls. one for each view. when the item is bound to the template set the visible property of each user control. 1 to true and the other to false.

one last note. there is a huge memory leak in your code related to ado.net. If your interested check out the FAQ section, there is an article on managing database connections. The same article is listed in my signature below.


Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top