如何在ASP.NET Web表单上编写C#代码? [英] How to write C# code on ASP.NET web form?

查看:69
本文介绍了如何在ASP.NET Web表单上编写C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是asp.net网站开发中的新手,

我无法在Asp.Net网络表单中实现我的逻辑..你能帮忙..



所以我有一个网络表单,我将使用转发器将数据绑定到表单。但我想在重复绑定值之前检查值来控制..



下面的代码



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

<%
 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

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

推荐答案

你需要深入了解开发webforms的心态。您只需添加控件并使用代码隐藏来操作这些控件,而不是将逻辑添加到aspx页面。所以我要做的是添加两个LinkBut​​ton控件,但将其可见性设置为false;



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: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>





然后使用代码隐藏页面中的ItemDataBound事件决定控件何时显示





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

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;
        }
    }
}





您还可以使用该事件来填充控件数据需要任何类型的逻辑来设置。



如果您使用的是DataTable,则代码如下所示





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

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;
        }
    }
}


这篇关于如何在ASP.NET Web表单上编写C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆