Asp.net 如何使动态列标题在gridview中可单击
我有一个包含许多列的gridview。它是可排序的,Allow Sorting=“True”,每列都有排序表达式。对于每个列,排序工作正常,除了我在Row_Databound event中指定的10个列具有动态标题外:Asp.net 如何使动态列标题在gridview中可单击,asp.net,sorting,gridview,webforms,Asp.net,Sorting,Gridview,Webforms,我有一个包含许多列的gridview。它是可排序的,Allow Sorting=“True”,每列都有排序表达式。对于每个列,排序工作正常,除了我在Row_Databound event中指定的10个列具有动态标题外: protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header)
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
if (Session["Label" + i.ToString()] !=null)
{
e.Row.Cells[i].Text = Session["Label" + i.ToString()].ToString();
}
}
}
}
protectedvoid gvSearchResults\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Header)
{
对于(int i=1;i<11;i++)
{
if(会话[“Label”+i.ToString()]!=null)
{
e、 Row.Cells[i].Text=Session[“Label”+i.ToString()].ToString();
}
}
}
}
这10列不可单击。有没有办法让它们可以点击?这些列中的所有其他内容都已启用排序
我从另一个论坛得到了一些关于在Page_Load或Page_Init事件中创建列的建议,但这可能对我不起作用
谢谢。这是可以做到的。如果您查看HTML代码,您将看到与此类似的链接,用于对GridView进行排序
<a href="javascript:__doPostBack('ctl00$mainContentPane$ctl02$GridView1','Sort$sortExpression')">yourColumnName</a>
我们需要在RowDataBound函数中重新创建该链接
for (int i = 1; i < 11; i++)
{
//first we cast the sender as a gridview
GridView gv = sender as GridView;
//get the unique ID of the gridview, this is different from ClientID which you normally would use for JavaScipt etc
string uniqueID = gv.UniqueID;
//then get the SortExpression for the column
string sortExpression = gv.Columns[i].SortExpression;
//get the new column name from the session
string yourColumnName = string.Empty;
if (Session["Label" + i.ToString()] != null)
{
yourColumnName = Session["Label" + i.ToString()].ToString();
}
//and then we fill the header with the new link
e.Row.Cells[i].Text = "<a href=\"javascript:__doPostBack('" + uniqueID + "','Sort$" + sortExpression + "')\">" + yourColumnName + "</a>";
}
for(int i=1;i<11;i++)
{
//首先,我们将发送者转换为gridview
GridView gv=发送方作为GridView;
//获取gridview的唯一ID,这与通常用于JavaScipt等的ClientID不同
字符串uniqueID=gv.uniqueID;
//然后获取列的SortExpression
string-sortExpression=gv.Columns[i].sortExpression;
//从会话中获取新列名
string yourColumnName=string.Empty;
if(会话[“Label”+i.ToString()]!=null)
{
yourColumnName=Session[“Label”+i.ToString()].ToString();
}
//然后我们用新链接填充标题
e、 Row.Cells[i].Text=“”;
}
但是,要使其工作,enableEventValidation必须设置为false,这是不推荐的。否则将出现“无效回发或回调参数”错误
最好在数据绑定到gridview之前以某种方式更改列名。您可以替换标题单元格中现有链接按钮的文本:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
string caption = Session["Label" + i.ToString()] as string;
if (caption != null)
{
TableCell headerCell = e.Row.Cells[i];
LinkButton lnkSort = headerCell.Controls[0] as LinkButton;
lnkSort.Text = caption;
}
}
}
}
protectedvoid gvSearchResults\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Header)
{
对于(int i=1;i<11;i++)
{
字符串标题=会话[“标签”+i.ToString()]作为字符串;
如果(标题!=null)
{
TableCell headerCell=e.Row.Cells[i];
LinkButton lnkSort=headerCell。控件[0]为LinkButton;
文本=标题;
}
}
}
}
非常感谢您的帮助。LinkButton的解决方案对我来说非常有用:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
if (Session["Label" + i.ToString()] !=null)
{
((LinkButton)(e.Row.Cells[i].Controls[0])).Text = Session["Label" + i.ToString()].ToString();
}
}
}
}
protectedvoid gvSearchResults\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Header)
{
对于(int i=1;i<11;i++)
{
if(会话[“Label”+i.ToString()]!=null)
{
((LinkButton)(例如行.单元格[i].控件[0])。Text=Session[“Label”+i.ToString()].ToString();
}
}
}
}
我对这个答案投了赞成票。我一点也没想到标题是一个链接按钮,也可以使用。这和我的答案是一样的,不是吗?如果是的话,你可以接受我的正确答案。对不起,我不知道怎么做,这是我在这里的第一篇帖子。我希望我做得对。是的,你做得对。我看到你第一次问问题;这就是我发表评论的原因。顺便说一句,欢迎使用StackOverflow.:-)