我想设置一个到网格中列的动态链接。我通过从 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);
}
要在 ASP.NET WebForms 中的 GridView 的列中设置动态链接,可以使用 TemplateField 控件。您可以在 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>
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);
}
}
}
}
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();
}
}