2023年2月15日 星期三

[研究][ASP.NET]GridView中傳送多個參數

[研究][ASP.NET]GridView中傳送多個參數

2023-02-15

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

********************************************************************************

用超連結網址 (HyperLink)傳

Default.aspx

<asp:GridView>
	<Columns>
          <asp:TemplateField ShowHeader="False">
		<asp:TemplateField HeaderText="編輯" InsertVisible="False" ItemStyle-Width="1%">
            <EditItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("id") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink_Edit" runat="server" 
                    NavigateUrl='<%# String.Format("~/Edit.aspx?Id={0}&Id2={1}", Eval("Id"), Eval("Id2")) %>'
                    CssClass="glyphicon glyphicon-pencil btn btn-primary btn-xs">編輯</asp:HyperLink>
            </ItemTemplate>
            <ItemStyle Wrap="False" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Edit.aspx.cs 中

protected void Page_Load(object sender, EventArgs e)
{
    if (!(String.IsNullOrEmpty(Request.QueryString["Id"])))
    {
        Int32.TryParse(Request.QueryString["Id"].ToString(), out id);
    }
	if (!(String.IsNullOrEmpty(Request.QueryString["Id2"])))
    {
        Int32.TryParse(Request.QueryString["Id2"].ToString(), out id2);
    }
}

********************************************************************************

用超連結按鈕 (LinkButton) 傳

Default.aspx

<asp:GridView>
	<Columns>
          <asp:TemplateField ShowHeader="False">
		<asp:TemplateField HeaderText="編輯" InsertVisible="False" ItemStyle-Width="1%">
            <EditItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("id") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" 
                    CommandName="Edit2" Text="修改" 
                    CommandArgument='<%# Eval("id3") + "," + Eval("id4") %>' 
                    CssClass="btn btn-primary btn-xs"></asp:LinkButton>
            </ItemTemplate>
            <ItemStyle Wrap="False" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Default.aspx.cs 中

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit2")
    {
        //string arg = e.CommandArgument.ToString();
        string[] arg = e.CommandArgument.ToString().Split();
		
        string id3 = e.CommandArgument.ToString().Split(',')[0].Trim();
	string id4 = e.CommandArgument.ToString().Split(',')[1].Trim() ;
    }
}				

********************************************************************************

用超連結按鈕 (LinkButton) 傳

Default.aspx

    <asp:GridView ID="GridView1" runat="server"
        CssClass="table table-striped table-bordered table-hover table-responsive" AllowPaging="True"
        AllowSorting="True" AutoGenerateColumns="False"
        OnPageIndexChanging="GridView1_PageIndexChanging" 
        DataKeyNames="id" 
        OnRowCommand="GridView1_RowCommand"
        DataSourceID="SqlDataSource1">
	<Columns>

	    <asp:TemplateField HeaderText="ID5" SortExpression="ID5" Visible="false">
	        <EditItemTemplate>
	            <asp:TextBox ID="TextBox_ID5" runat="server" Text='<%# Bind("ID5") %>'></asp:TextBox>
	        </EditItemTemplate>
	        <ItemTemplate>
	            <asp:Label ID="Label_ID5" runat="server" Text='<%# Bind("ID5") %>'></asp:Label>
	        </ItemTemplate>
	    </asp:TemplateField>
	    <asp:TemplateField HeaderText="ID6" SortExpression="ID6" Visible="false">
	        <EditItemTemplate>
	            <asp:TextBox ID="TextBox_ID6" runat="server" Text='<%# Bind("ID6") %>'></asp:TextBox>
	        </EditItemTemplate>
	        <ItemTemplate>
	            <asp:Label ID="Label_ID6" runat="server" Text='<%# Bind("ID6") %>'></asp:Label>
	        </ItemTemplate>
	    </asp:TemplateField>

        <asp:TemplateField ShowHeader="False">
	    <asp:TemplateField HeaderText="編輯" InsertVisible="False" ItemStyle-Width="1%">
            <EditItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("id") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" CommandName="Edit2" Text="編輯"  />
            </ItemTemplate>
            <ItemStyle Wrap="False" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Default.aspx.cs 中

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit2")
    {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView1.Rows[rowIndex];
                string id5 = (row.FindControl("Label_ID5") as Label).Text;
                string id6 = (row.FindControl("Label_ID6") as Label).Text;
                //string id5 = GridView1.Rows[rowIndex].Cells[1].Text;
    }
}				


(完)

相關

沒有留言:

張貼留言