How to pass multiple values with Link Button Insid

2019-08-10 22:31发布

问题:

I have a Link Button Inside Repeater Control and I want pass to another page

<ItemTemplate>
    <tr>
        <td bgcolor="#D8E1EA" style="display: none">
            <asp:Label ID="empId" runat="server" Text='<% # Eval("emp_id")%>' />
        </td>
        <td>
            <asp:LinkButton ID="lnkRowSelection" runat="server" Text='<% # Eval("FullEmpName")%>' CausesValidation="false"
                CssClass="navigationlink" CommandName='<% # Eval("FullEmpName")%>' CommandArgument='<%#Eval("emp_id") %>'
                OnClick="ButtonLink_Click" ForeColor="black" Font-Underline="false" />
        </td>
        <td>
            <asp:Label ID="Label1" runat="server" Text='<% # Eval("days_worked")%>' />
        </td>
        <td>
            <asp:Label ID="Label2" runat="server" Text='<% # Eval("total_absents")%>' />
        </td>
        <td>
            <asp:Label ID="Label5" runat="server" Text='<% # Eval("days_marked")%>' />
        </td>
    </tr>
</ItemTemplate>

now I want to pass multiple values to Other Page (ManualAttendance.aspx) with Link Button I have tried this but with this i can only pass one value lnkRowSelection.Text Please tell me how can i pass multiple column values?? this is what i have tried

protected void ButtonLink_Click(object sender, System.EventArgs e)
{
    //LinkButton _lnkBtn = rptTeamAverage.Items[i].FindControl("lnkRowSelection") as LinkButton;
    LinkButton lnkRowSelection = (LinkButton)sender;
    //Label empId = (Label)sender;

    lnkRowSelection.PostBackUrl = "ManualAttendance.aspx?id=" + lnkRowSelection.Text + "&M=" + Request.QueryString["M"] + "&S=" + int.Parse(Request.QueryString["S"].ToString()) + "&sys=" + ((System.Collections.Generic.Dictionary<string, string>)Session["User"])["Token"];
}

回答1:

You can use semicolon separated values in the CommandArgument property of lnkRowSelection. Let's say you want to pass values of emp_id, days_worked, total_absents, and days_marked, here's what you need to add in the aspx code:

<asp:LinkButton ID="LinkButton1" 
    runat="server" 
    Text='<% # Eval("FullEmpName") %>' 
    CausesValidation="false"
    CssClass="navigationlink" 
    CommandName='<% # Eval("FullEmpName") %>' 
    CommandArgument='<%#Eval("emp_id") + ";" + Eval("days_worked") + ";" + Eval("total_absents") + ";" + Eval("days_marked") %>'
    OnClick="ButtonLink_Click" 
    ForeColor="black" 
    Font-Underline="false" />

then in ButtonLink_Click:

protected void ButtonLink_Click(object sender, System.EventArgs e)
{
    LinkButton lnkRowSelection = (LinkButton)sender;
    string[] arguments = lnkRowSelection.CommandArgument.Split(';');
    string emp_id = arguments[0];
    string days_worked = arguments[1];
    string total_absents = arguments[2];
    string days_marked = arguments[3];

    // pass emp_id, days_worked, total_absents, and days_marked to another page via query string
    Response.Redirect(string.Format("ManualAttendance.aspx?id={0}&daysworked={1}&totalabsents={2}&daysmarked={3}", emp_id, days_worked, total_absents, days_marked), false);
}