Question

Hey guys I have made a repeater which shows the Employee ID and the leaves taken by him in a particular month. Now my repeater Works fine. Just I want to show the empID which the user entered in the textbox as repeater header. My repeater Code is

> <table class="table1" >
>     <asp:Repeater ID="Repeater1" runat="server">
>        <HeaderTemplate>
>            <tr>
>                 <td>EmpID</td>
>        </HeaderTemplate>
>        <ItemTemplate>
>         <td><asp:Label ID="lblmonths" runat="server" Text='<%# Eval("Monthnames") %>' ></asp:Label></td>
>        </ItemTemplate>
>        <FooterTemplate>
>             </tr>
>        </FooterTemplate>
         </asp:Repeater>
>                 <asp:Repeater ID="Repeater2"  runat="server" >
>                 <HeaderTemplate>
>                 <tr>
>                         <td><asp:Label ID="empID" runat="server" Text='<%# Eval("EmpID") %>' ></asp:Label></td>
>                 </HeaderTemplate>
>                     <ItemTemplate>
>                         <td><asp:Label ID="leave" runat="server" Text='<%# Eval("Leaves") %>' ></asp:Label></td>
>                     </ItemTemplate>
>                      <FooterTemplate>
>                  </tr>        
>                      </FooterTemplate>  
>                 </asp:Repeater>
>                 
>             
>     </table>

and my code behind is

string cmdText3 = "select count(*) as Leaves from attendance where empid = '" + empID.Value + "' and extract(month from LeaveDate) between 1 and 6 group by extract(month from LeaveDate)";
                MySqlCommand cmd3 = new MySqlCommand(cmdText3, cnx);
                MySqlDataAdapter adapter3 = new MySqlDataAdapter();
                DataSet ds3 = new DataSet();
                adapter3.SelectCommand = cmd3;
                adapter3.Fill(ds3);
                DataTable dt3 = ds3.Tables[0];
                Repeater2.DataSource = dt3;
                Repeater2.DataBind();

Here am showing 6 months. Now this code works fine but the EmpID is not shown. Can you tell me how to show the empID from the text box in this label(<asp:Label ID="empID" runat="server" Text='<%# Eval("EmpID") %>' ></asp:Label>). I am not familiar with datatable which is shown in code behind. May be adding a column there would help.I tried but nothing happened. Can you help me with this ?

Was it helpful?

Solution 2

Firstly, it doesn't look like you are returning a column named "EmpID". This is an issue with your SQL statement and, IMHO, is outside of the scope of this question. That being said, once you fix the SQL, here is how you would go about binding a label in the header of the repeater.

You can use the repeater's OnItemDataBound method and look for the e.Item.ItemType of Header.

First, bind the repeater to the OnItemDataBound event. You can do this on the client-side or the server side:

  1. Client Side:

    <asp:Repeater ID="rptSample" runat="server" OnItemDataBound="rptSample_ItemDataBound">
    
  2. Server Side:

    protected override void OnInit(EventArgs e)   
    {
       base.OnInit(e);
       this.rptSample.ItemDataBound += rptSample_ItemDataBound;
    }
    

Then you need to check the type of item being bound in the ItemDataBound method. Like so:

    protected void rptSample_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {

        }
    }

To be able to access the datasource (dt3) from inside the rptSample_ItemDataBound method, you need to make it a page-level variable. So alter the following line:

DataTable dt3 = ds3.Tables[0];

Place the following line so it is a page-level variable

 DataTable dt3 = null; 

and change the dt3 assignment line to this:

dt3 = ds3.Tables[0];

Then your ItemDataBound method will look like this:

    protected void rptSample_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            ((Label)e.Item.FindControl("lblmonths")).Text = dt3.Rows[0]["EmpId"].ToString();
        }
    }

OTHER TIPS

try not to use <%#Eval %> syntax in the HeaderTemplate, you should instead try to define some method or property to access The field you're trying to access using <%=MyEmpIdProperty %>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top