我想向网格的一列添加动态链接属性,该列从另一列获取参数

问题描述 投票:0回答:1

我想设置一个到网格中列的动态链接。我通过从 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);
                }
c# asp.net webforms
1个回答
0
投票

要在 ASP.NET WebForms 中的 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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.