Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello, I am Vikas new in asp.net web development,
I am unable to implement my logic in Asp.Net web form.. can you help..

So I have a web form I am going to bind data to form by using repeater. but i want to check value before repeter bind the value to control..

Se below code

ASP.NET
<%
 if (Convert.ToString(Eval("InterestStatus")) == "Pendding")
         {
               
         }
   %><asp:LinkButton ID="lbtnAccept" CommandName="AcceptInterest" runat="server">ACCEPT</asp:LinkButton>&nbsp &nbsp
                            <asp:LinkButton ID="lbtnReject" CommandName="RejectInterest" runat="server">REJECT</asp:LinkButton>


In above code I want to check
C#
if (Convert.ToString(Eval("InterestStatus")) == "Pendding")

then I want visible below link buttons else not.

What I have tried:

I am trying to Accept Reject request from one user to another.
if request get accept then it would be seen there Accepted..

if any query to understanding my problem please ask...

Thank you
Posted
Updated 28-Nov-16 1:15am

1 solution

You need to get into the proper mindset for developing webforms. Rather than add logic to the aspx page you simply add the controls and using the code-behind to manipulate these controls. So what I'd do is add both the LinkButton controls but set their visibility to false;

ASP.NET
<asp:Repeater ID="MyRepeater" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="lbtnAccept" CommandName="AcceptInterest" Visible="false" runat="server">ACCEPT</asp:LinkButton>&nbsp &nbsp
        <asp:LinkButton ID="lbtnReject" CommandName="RejectInterest" Visible="false" runat="server">REJECT</asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>


Then use the ItemDataBound event in the code-behind page to decide when the controls are shown

C#
protected void Page_Load(object sender, EventArgs e)
{
    List<MyData> data = new List<MyData> { new MyData { InterestStatus = "ABC" }, new MyData { InterestStatus="Pending" } };

    MyRepeater.DataSource = data;
    MyRepeater.ItemDataBound += MyRepeater_ItemDataBound;
    MyRepeater.DataBind();
}

protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        MyData rowData = (MyData)e.Item.DataItem;

        if (rowData.InterestStatus == "Pending")
        {
            LinkButton lbtnAccept = (LinkButton)e.Item.FindControl("lbtnAccept");
            LinkButton lbtnReject = (LinkButton)e.Item.FindControl("lbtnReject");

            lbtnAccept.Visible = true;
            lbtnReject.Visible = true;
        }
    }
}


You can also use that event to populate the controls if their data needs any kind of logic to set.

If you're using a DataTable then the code would be like below

C#
protected void Page_Load(object sender, EventArgs e)
{
    DataTable data = new DataTable();
    data.Columns.Add("ID", typeof(int));
    data.Columns.Add("Name", typeof(string));
    data.Columns.Add("InterestStatus", typeof(string));

    data.Rows.Add(1, "Joe", "Approved");
    data.Rows.Add(2, "Dave", "Pending");

    MyRepeater.DataSource = data;
    MyRepeater.ItemDataBound += MyRepeater_ItemDataBound;
    MyRepeater.DataBind();
}

protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // The ItemDataBound event is called once for each "template" in the repeater
    // So it is called when binding the header, the footer and the items
    // We're only interesting when it is binding the items so we use this if
    // to make sure our code only runs when it is something in the ItemTemplate that
    // is being bound.  Rather than there being a single type for item there are two,
    // Item and AlternatingItem, this is incase what what to do something different
    // for alternative items, ie even and odd rows, such as have different background
    // colours etc.
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        // The item of data being bound to the item is in e.Item.DataItem so we have
        // to cast it to its proper type in order to use it.  If you are binding to a DataTable
        // then this is DataRowView and it represents the data in that individual row
        DataRowView rowData = (DataRowView)e.Item.DataItem;

        // We access the fields of rowData by putting the fieldname in square brackets
        // again we need to cast to the proper type
        if ((string)rowData["InterestStatus"] == "Pending")
        {
            LinkButton lbtnAccept = (LinkButton)e.Item.FindControl("lbtnAccept");
            LinkButton lbtnReject = (LinkButton)e.Item.FindControl("lbtnReject");

            lbtnAccept.Visible = true;
            lbtnReject.Visible = true;
        }
    }
}
 
Share this answer
 
v2
Comments
Vikas Hire 29-Nov-16 1:46am    
I bound a Data Table to my repeater,
how can I check the repeater value.. using if condition

I little bit confused about..


if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
MyData rowData = (MyData)e.Item.DataItem;
F-ES Sitecore 29-Nov-16 4:03am    
I've updated the solution to include code for binding to a DataTable along with some addition comments to cover those lines of code.
Vikas Hire 29-Nov-16 4:25am    
Thank You.. Its Worked Out..
Thank you so much...for giving me your valuable time..
Vikas Hire 29-Nov-16 3:54am    
Thank you... F-ES Sitecore

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900