我有一个大GridView
和HyperLinkfield
绑定到其中一个列的页面,其中包含两个参数,格式如下:
<asp:HyperLinkField DataNavigateUrlFields="id,nome" DataNavigateUrlFormatString="~/adm/Clipping/Publicidade/Cadastro/ValorPrograma.aspx?programa={0}&nome={1}" HeaderText="Valores" InsertVisible="False" NavigateUrl="~/adm/Clipping/Publicidade/Cadastro/ValorPrograma.aspx" Text="Ajustar valores">
<ItemStyle ForeColor="#339933" />
</asp:HyperLinkField>
字符串DataNavigateUrlFormatString="~/adm/Clipping/Publicidade/Cadastro/ValorPrograma.aspx?programa={0}&nome={1}
已替换为DataNavigateUrlFields="id,nome"
。对于某些行,一切都很好 ...另一方面,值不会被替换,URL也不完整。
所以我去数据库检查是否存在一些数据不一致,并从GridView
上正常替换的字段和未被替换的另一个字段中提取数据。
有什么想法吗?
答案 0 :(得分:2)
根据Hanno的comments建议并由jadarnel27的实际Reply回复,问题与网址字符编码有关(特别是在这种情况下编码) :
个字符。
要解决此问题,我建议使用TemplateField
代替HyperLink
字段,如下所示
<asp:TemplateField HeaderText="Valores" InsertVisible="False">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "~/adm/Clipping/Publicidade/Cadastro/ValorPrograma.aspx?programa=" + HttpUtility.UrlEncode(Eval("id").ToString()) + "&nome=" + HttpUtility.UrlEncode(Eval("nome").ToString()) %>'
Text="Ajustar valores"></asp:HyperLink>
</ItemTemplate>
<ItemStyle ForeColor="#339933" />
</asp:TemplateField>
关键概念是在模板中使用HttpUtility.UrlEncode()
而不是调用RowDataBound
事件。
完成上述操作后,数据库数据将在编写URL之前进行正确编码,并且正常工作。
答案 1 :(得分:0)
我同意Hanno's声明in the comments,这可能是由于网址字符嵌入。
这是一种可以对RowDataBound事件中的字符进行编码的方法:
protected void yourGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
i = 0; // the ordinal of the column you need to encode
DataRowView rowView = (DataRowView)e.Row.DataItem;
string id = Server.UrlEncode(rowView["id"].ToString());
string nome = Server.UrlEncode(rowView["nome"].ToString());
string newURL = String.Format("~/adm/Clipping/Publicidade/Cadastro/ValorPrograma.aspx?programa={0}&nome={1}", id, nome);
e.Row.Cells[i].Text = newURL;
}
}
您需要在GridView
的标记中包含类似内容,以便将其连接到事件处理程序:
OnRowDataBound="yourGridView_RowDataBound"