asp.net 我想将动态链接属性添加到网格的某列,该列从另一列获取参数

xdnvmnnf  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(67)

我想设置一个动态链接到网格中的列。我通过从webservis阅读来填充.cs文件中的网格我可以显示页面上的数据但不能设置到列的动态链接我该怎么做?
我如何填充网格如下;

public DataTable FillGrid(decimal CurrentUserID)
    {        
        SasBanaUygunIslerEkleRequest sasBanaUygunIslerEkleRequest = new SasBanaUygunIslerEkleRequest();
        sasBanaUygunIslerEkleRequest.IstProfilKayitNo = ((int)CurrentUserID);
        SasLocalServiceBLL sasLocalServiceBLL = new SasLocalServiceBLL();
        ServisSonucBE<List<SasBanaUygunIslerEkleResponse>> resp = sasLocalServiceBLL.BanaUygunIslerEkle(sasBanaUygunIslerEkleRequest);

        DataTable dt = new DataTable();
        dt.Columns.Add("TALEPNO", typeof(string));
        dt.Columns.Add("BASLANGICTARIHI", typeof(string));
        dt.Columns.Add("BITISTARIHI", typeof(string));

        for (int index = 0; index < resp.Veri.Count; index++)
        {
            SasBanaUygunIslerEkleResponse var;
            DataRow row = dt.NewRow();
            if (resp.Veri.ToArray()[index].IstIsgucuIstemiKayitNo != null)
            {
                row[0] = resp.Veri.ToArray()[index].IstIsgucuIstemiKayitNo;
            }

            dt.Rows.Add(row);

        }
        return dt;
    }

字符串
我想将链接属性设置为第一个字段。我想根据条件改变链接页。

if (SecurityContext.IsUser && SecurityContext.CurrentUser != null && SecurityContext.CurrentUser.IsInternalUser)
                {
                    object isverenTur = DataBinder.GetPropertyValue(e.Row.DataItem, "ISVERENTUR");
                    object userId = CurrentQueryHashedUserID;
                    if (userId != null)
                    {
                        literal.Text = string.Format("<a href=\"javascript:PopupJobDetailsImd('{0}','{1}','{2}');\">{0}</a>", isgucuIstemiNo, isverenTur, userId);
                    }
                    else
                        literal.Text = string.Format("<a href=\"javascript:PopupJobDetails('{0}');\">{0}</a>", isgucuIstemiNo);

                }
                else
                {
                    literal.Text = string.Format("<a href=\"javascript:PopupJobDetails('{0}');\">{0}</a>", isgucuIstemiNo);
                }

iyfjxgzm

iyfjxgzm1#

若要在ASP.NETWebForms中的GridView列中设置动态链接,可以使用TemplateField控件。您可以在TemplateField中定义一个模板,而不是直接将列绑定到数据,从而允许您自定义内容并包含动态链接。
以下是您的操作方法:

  • 在.aspx页中,添加一个GridView控件,并为要在其中放置动态链接的列定义一个TemplateField。
<asp:GridView ID="YourGridView" runat="server">
    <Columns>
        <asp:TemplateField HeaderText="TALEPNO">
            <ItemTemplate>
                <asp:Literal ID="literal" runat="server"></asp:Literal>
            </ItemTemplate>
        </asp:TemplateField>

        <!-- Add other columns here -->
    </Columns>
</asp:GridView>

字符串

  • 在代码隐藏(.cs文件)中,处理GridView的RowDataBound事件。在此事件中,可以根据您的条件动态设置Literal控件的内容并创建动态链接。
protected void YourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView rowView = e.Row.DataItem as DataRowView;
        if (rowView != null)
        {
            // Access the data for the current row
            string isgucuIstemiNo = rowView["TALEPNO"].ToString();
            string isverenTur = rowView["ISVERENTUR"].ToString();
            object userId = CurrentQueryHashedUserID;

            Literal literal = (Literal)e.Row.FindControl("literal");

            if (SecurityContext.IsUser && SecurityContext.CurrentUser != null && SecurityContext.CurrentUser.IsInternalUser)
            {
                if (userId != null)
                {
                    literal.Text = string.Format("<a href=\"javascript:PopupJobDetailsImd('{0}','{1}','{2}');\">{0}</a>", isgucuIstemiNo, isverenTur, userId);
                }
                else
                {
                    literal.Text = string.Format("<a href=\"javascript:PopupJobDetails('{0}');\">{0}</a>", isgucuIstemiNo);
                }
            }
            else
            {
                literal.Text = string.Format("<a href=\"javascript:PopupJobDetails('{0}');\">{0}</a>", isgucuIstemiNo);
            }
        }
    }
}

  • 不要忘记在.cs文件的Page_Load方法中连接RowDataBound事件:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Bind your GridView here
        decimal currentUserId = GetCurrentUserId(); // Replace this with the actual way to get the user ID
        DataTable dt = FillGrid(currentUserId);
        YourGridView.DataSource = dt;
        YourGridView.DataBind();
    }
}

相关问题