GridView模板列 \ HyperLink \ LinkButton 演练
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ProductID,ProductName"
AutoGenerateColumns=False onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">
<ItemTemplate>
<asp:LinkButton runat=server CommandName=ProcName Text='<%# Eval("ProductName") %>' CommandArgument='<%#Eval("ProductID")%>'></asp:LinkButton>
<a href="Default.aspx?id=<%#Eval("ProductID")%>&name=<%# Eval("ProductName") %>" target=_blank><%# Eval("ProductName") %></a>
<asp:HyperLink ID="link" runat="server" Text='<%# Eval("ProductName") %>' NavigateUrl="~/Default.aspx" ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
SortExpression="CategoryID" />
</Columns>
</asp:GridView>
AutoGenerateColumns=False onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">
<ItemTemplate>
<asp:LinkButton runat=server CommandName=ProcName Text='<%# Eval("ProductName") %>' CommandArgument='<%#Eval("ProductID")%>'></asp:LinkButton>
<a href="Default.aspx?id=<%#Eval("ProductID")%>&name=<%# Eval("ProductName") %>" target=_blank><%# Eval("ProductName") %></a>
<asp:HyperLink ID="link" runat="server" Text='<%# Eval("ProductName") %>' NavigateUrl="~/Default.aspx" ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
SortExpression="CategoryID" />
</Columns>
</asp:GridView>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string sName = e.CommandName;
int sArg = int.Parse(e.CommandArgument.ToString());
Response.Write(sName + " - " + sArg);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
Control ctrl = e.Row.Cells[2].FindControl("link");
HyperLink link = ctrl as HyperLink;
if (link != null)
{
string sID = this.GridView1.DataKeys[index].Values[0].ToString();
string sName = this.GridView1.DataKeys[index].Values[1].ToString();
link.NavigateUrl += "?id=" + sID + "&name=" + sName;
}
}
}
{
string sName = e.CommandName;
int sArg = int.Parse(e.CommandArgument.ToString());
Response.Write(sName + " - " + sArg);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
Control ctrl = e.Row.Cells[2].FindControl("link");
HyperLink link = ctrl as HyperLink;
if (link != null)
{
string sID = this.GridView1.DataKeys[index].Values[0].ToString();
string sName = this.GridView1.DataKeys[index].Values[1].ToString();
link.NavigateUrl += "?id=" + sID + "&name=" + sName;
}
}
}
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号