You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为WebService填充的Grid列添加带条件的动态链接?

实现Grid第一列动态权限链接的方案
  • 补充DataTable所需字段
    首先要确保填充Grid的FillGrid方法返回的DataTable包含链接逻辑需要的ISVERENTUR字段,因为后续要用到这个值生成链接:

    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));
        // 新增ISVERENTUR字段,用于链接逻辑
        dt.Columns.Add("ISVERENTUR", typeof(string));
    
        for (int index = 0; index < resp.Veri.Count; index++)
        {
            var item = resp.Veri[index];
            DataRow row = dt.NewRow();
            if (item.IstIsgucuIstemiKayitNo != null)
            {
                row["TALEPNO"] = item.IstIsgucuIstemiKayitNo;
            }
            // 填充ISVERENTUR字段(根据实际响应模型字段名调整)
            row["ISVERENTUR"] = item.IsverenTur; 
            dt.Rows.Add(row);
        }
        return dt;
    }
    
  • 在Grid的RowDataBound事件中生成动态链接
    在页面后台代码中,添加Grid控件的RowDataBound事件处理逻辑,将权限判断和链接生成逻辑放在这里:

    protected void YourGridID_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // 仅处理数据行,跳过表头、页脚
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            string isgucuIstemiNo = rowView["TALEPNO"].ToString();
            string isverenTur = rowView["ISVERENTUR"].ToString();
            object userId = CurrentQueryHashedUserID;
    
            // 创建Literal控件承载链接
            Literal literal = new 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);
            }
    
            // 将链接替换第一列原有内容
            e.Row.Cells[0].Controls.Clear();
            e.Row.Cells[0].Controls.Add(literal);
        }
    }
    
  • 关联Grid的RowDataBound事件
    确保前端Grid控件绑定了上述事件:

    <asp:GridView ID="YourGridID" runat="server" OnRowDataBound="YourGridID_RowDataBound">
        <Columns>
            <asp:BoundField DataField="TALEPNO" HeaderText="申请编号" />
            <asp:BoundField DataField="BASLANGICTARIHI" HeaderText="开始日期" />
            <asp:BoundField DataField="BITISTARIHI" HeaderText="结束日期" />
        </Columns>
    </asp:GridView>
    

关键说明

  • 需确认SasBanaUygunIslerEkleResponse模型中存在IsverenTur(或对应ISVERENTUR的字段),字段名不一致时需同步修改代码。
  • RowDataBound是GridView动态修改行内容的标准触发点,能基于每行数据和用户权限生成个性化链接。
  • 使用Literal控件承载HTML链接,可避免直接拼接HTML的编码问题,保证链接正常渲染。

内容的提问来源于stack exchange,提问作者Haluk Öztürk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 15:02:08