如何更改提交按钮以更新按钮

时间:2013-03-18 10:55:41

标签: asp.net button gridview postback

我们有两页在第一页上我有我的表格,第二页是我的表格值存储在网格中的报告页面。我现在在我的表单页面上提交按钮当我点击编辑链接时,我将重定向到我的表单页面,我可以在其中进行更改和更新详细信息。现在我想要的是当我点击编辑链接并移动到我的表单页面进行编辑时我的提交按钮更改为更新按钮我该怎么做...

这是我的第一页代码:

                   选择            TRAVELONG            ONETRAVEL            .UK-BSP            .CA-YYZ            .CA-YVR            部分MCO退款            

<div id="FirstForm" style="margin-left: 80px" runat="server">
<table class="style1">
       <tr>
           <td class="style13">
               <asp:Label ID="lblTid" runat="server" Text="TID"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbTid" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvTid" runat="server" 
                   ControlToValidate="tbTid" ErrorMessage="Enter Tid" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblUnusedTicketAmount" runat="server" 
                   Text="Unused Ticket Amount"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbUnusedTicketAmount" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvUnusedTicketAmount" runat="server" 
                   ControlToValidate="tbUnusedTicketAmount" ErrorMessage="Enter Amount" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               <asp:DropDownList ID="ddlUnusedAmount" runat="server">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style14">
               <asp:Label ID="lblPNR" runat="server" Text="PNR"></asp:Label>
           </td>
           <td class="style9">
               <asp:TextBox ID="tbPNR" runat="server"></asp:TextBox>
           </td>
           <td class="style8">
               <asp:RequiredFieldValidator ID="rfvPNR" runat="server" 
                   ControlToValidate="tbPNR" ErrorMessage="Enter PNR" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style9">
               <asp:Label ID="lblAirlinePenality" runat="server" Text="Airline Penality"></asp:Label>
           </td>
           <td class="style12">
               <asp:TextBox ID="tbAirlinePenality" runat="server"></asp:TextBox>
           </td>
           <td class="style10">
               <asp:RequiredFieldValidator ID="rfvAirlinePenality" runat="server" 
                   ControlToValidate="tbAirlinePenality" ErrorMessage="Enter Penality" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style10">
               <asp:DropDownList ID="ddlAirlinePenality" runat="server" AutoPostBack="True">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="lblTicketNumber" runat="server" Text="Ticket Number"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbTicketNumber" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvTicketNumber" runat="server" 
                   ControlToValidate="tbTicketNumber" ErrorMessage="Enter ESAC" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblNetRefundProcess" runat="server" Text="Net Refund Process"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbNetRefundProcess" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvNetRefundProcess" runat="server" 
                   ControlToValidate="tbNetRefundProcess" ErrorMessage="RequiredFieldValidator" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               <asp:DropDownList ID="ddlNetRefundProcess" runat="server" AutoPostBack="True">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="lblESACCode" runat="server" Text="ESAC Code"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbESACCode" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvESACCode" runat="server" 
                   ControlToValidate="tbESACCode" ErrorMessage="Enter ESAC code" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblRefundableCommision" runat="server" 
                   Text="Refundable Commission"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbRefundableCommision" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvRefundableCommission" runat="server" 
                   ControlToValidate="tbRefundableCommision" ErrorMessage="RequiredFieldValidator" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               <asp:DropDownList ID="ddlRefundableCommission" runat="server" 
                   AutoPostBack="True">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="lblWaiverCode" runat="server" Text="Waiver Code"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbWaiverCode" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvWaiverCode" runat="server" 
                   ControlToValidate="tbWaiverCode" ErrorMessage="Enter Waiver Code" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblCouponRefunded" runat="server" Text="Coupon Refunded"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbCouponRefund" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvCouponRefunded" runat="server" 
                   ControlToValidate="tbCouponRefund" ErrorMessage="Enter Coupon Refund" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               &nbsp;</td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="Label7" runat="server" Text="Remarks"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbRemarks" runat="server" TextMode="MultiLine"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvRemarks" runat="server" 
                   ControlToValidate="tbRemarks" ErrorMessage="Enter Remarks" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblRefundType" runat="server" Text="Refund Type"></asp:Label>
           </td>
           <td class="style11">
               <asp:DropDownList ID="ddlRefundType" runat="server">
                   <asp:ListItem>Full</asp:ListItem>
                   <asp:ListItem>Partial</asp:ListItem>
               </asp:DropDownList>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvRefundType" runat="server" 
                   ControlToValidate="ddlRefundType" ErrorMessage="Select Refund Type" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               &nbsp;</td>
       </tr>
       <tr>
           <td class="style13" colspan="7">
    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit"/>
               <asp:Button ID="btnReset" runat="server" onclick="btnReset_Click" Text="Reset"/>                
           </td>
       </tr>
   </table>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

                   

aspx.cs页面代码:

    protected void  btnSubmit_Click(object sender, EventArgs e)
  {
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Bart;Integrated Security=True");
    con.Open();

    string Portal = ddlPortal.SelectedValue;
    string TID = tbTid.Text;
    string PNR = tbPNR.Text;
    string TicketNumber = tbTicketNumber.Text;
    string ESACCode = tbESACCode.Text;
    string WaiverCode = tbWaiverCode.Text;
    string Remarks = tbRemarks.Text;
    string UnusedTicketAmount = tbUnusedTicketAmount.Text;
    string UnusedAmount = ddlUnusedAmount.SelectedValue;
    string AirlinePenality = tbAirlinePenality.Text;
    string Airline = ddlAirlinePenality.SelectedValue;
    string NetRefundProcess = tbNetRefundProcess.Text;
    string NetRefund = ddlNetRefundProcess.SelectedValue;
    string RefundableCommission = tbRefundableCommision.Text;
    string Refundable = ddlRefundableCommission.SelectedValue;
    string CouponRefunded = tbCouponRefund.Text;
    string RefundType = ddlRefundType.SelectedValue;

    using (SqlCommand cmd = con.CreateCommand())
    {
        cmd.CommandText = "insert into Form3(Portal,TID,PNR,TicketNumber,ESACCode,WaiverCode,Remarks,UnusedTicketAmount,ddlUnusedAmount, AirlinePenality, ddlAirlinePenality, NetRefundProcess, ddlNetRefundProcess, RefundableCommission, ddlRefundableCommission, CouponRefunded,RefundType) values('" + Portal + "','" + TID + "','" + PNR + "','" + TicketNumber + "', '" + ESACCode + "', '" + WaiverCode + "', '" + Remarks + "','" + UnusedTicketAmount + "','" + UnusedAmount + "','" + AirlinePenality + "','" + Airline + "','" + NetRefundProcess + "','" + NetRefund + "','" + RefundableCommission + "','" + Refundable + "','" + CouponRefunded + "','" + RefundType + "')";
        cmd.Parameters.AddWithValue("@Portal", ddlPortal.SelectedIndex);
        cmd.Parameters.AddWithValue("@TID", tbTid.Text.Trim());
        cmd.Parameters.AddWithValue("@PNR", tbPNR.Text.Trim());
        cmd.Parameters.AddWithValue("@TicketNumber", tbTicketNumber.Text.Trim());
        cmd.Parameters.AddWithValue("@ESACCode", tbESACCode.Text.Trim());
        cmd.Parameters.AddWithValue("@WaiverCode", tbWaiverCode.Text.Trim());
        cmd.Parameters.AddWithValue("@Remarks", tbRemarks.Text.Trim());
        cmd.Parameters.AddWithValue("@UnusedTicketAmount", tbUnusedTicketAmount.Text.Trim());
        cmd.Parameters.AddWithValue("@ddlUnusedAmount", ddlUnusedAmount.SelectedIndex);
        cmd.Parameters.AddWithValue("@AirlinePenality", tbAirlinePenality.Text.Trim());
        cmd.Parameters.AddWithValue("@ddlAirlinePenality", ddlAirlinePenality.SelectedIndex);
        cmd.Parameters.AddWithValue("@NetRefundProcess", tbNetRefundProcess.Text.Trim());
        cmd.Parameters.AddWithValue("@ddlNetRefundProcess", ddlNetRefundProcess.SelectedIndex);
        cmd.Parameters.AddWithValue("@RefundableCommission", tbRefundableCommision.Text.Trim());
        cmd.Parameters.AddWithValue("@ddlRefundableCommission", ddlRefundableCommission.SelectedIndex);
        cmd.Parameters.AddWithValue("@CouponRefunded", tbCouponRefund.Text.Trim());
        cmd.Parameters.AddWithValue("@RefundType", ddlRefundType.SelectedIndex);
        cmd.ExecuteNonQuery();
    }
    con.Close();
    tbTid.Text = "";
    tbPNR.Text = "";
    tbTicketNumber.Text = "";
    tbESACCode.Text = "";
    tbWaiverCode.Text = "";
    tbRemarks.Text = "";
    tbRemarks.Text = "";
    tbUnusedTicketAmount.Text = "";
    tbAirlinePenality.Text = "";
    tbNetRefundProcess.Text = "";
    tbRefundableCommision.Text = "";
    tbCouponRefund.Text = "";
    lblRefundType.Text = "";
    tbTid.Focus();
}

第二个报告页面::

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" AutoGenerateColumns="False" 
    onrowcommand="GridView1_RowCommand1">
    <Columns>
       <asp:TemplateField HeaderText="Query">
            <itemtemplate>
            <asp:LinkButton CommandName="cmdBind"  runat="server"  Text='<%#Eval("ID")%>' ID="ID" ToolTip='<%#Eval("ID")%>'>LinkButton
            </asp:LinkButton>                                                                   
            </itemtemplate>
            </asp:TemplateField>            
       <asp:BoundField DataField="Portal" HeaderText="Portal" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="TID" HeaderText="TID" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="PNR" HeaderText="PNR" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="TicketNumber" HeaderText="Ticket Number" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="ESACCode" HeaderText="ESACCode" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="WaiverCode" HeaderText="WaiverCode" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="Remarks" HeaderText="Remarks" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="UnusedTicketAmount" HeaderText="UnusedTicketAmount" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="ddlUnusedAmount" HeaderText="ddlUnusedAmoun" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="AirlinePenality" HeaderText="AirlinePenality" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="ddlAirlinePenality" HeaderText="ddlAirlinePenality" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="NetRefundProcess" HeaderText="NetRefundProcess" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="ddlNetRefundProcess" HeaderText="ddlNetRefundProcess" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="RefundableCommission" HeaderText="RefundableCommission" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="ddlRefundableCommission" HeaderText="ddlRefundableCommission" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="CouponRefunded" HeaderText="CouponRefunded" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="RefundType" HeaderText="RefundType" ItemStyle-HorizontalAlign="Center"/>
    </Columns>       
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
报告的

aspx.cs页面:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!Page.IsPostBack)
        {
            cmd = new SqlCommand("select * from Form3", con);
            da = new SqlDataAdapter(cmd);
            dt.Clear();
            con.Open();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            con.Close();
        }
    }
    catch (Exception ex)
    {

        Response.Write(ex.ToString());
    }

}
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "cmdBind")
    {
        LinkButton lb = (LinkButton)e.CommandSource;

        Response.Redirect("Form.aspx?ID=" + lb.Text + "");
    }
}

1 个答案:

答案 0 :(得分:1)

您需要使用State Management技术来实现此目的。我使用Session来演示。

在您拥有Submit按钮的页面上,您需要创建property,如下所示:

public string ButtonTextValue  
{
    get 
    {
        if (Session["ButtonValue"] == null)
            Session["ButtonValue"] = "Submit";

            return Convert.ToString(Session["ButtonValue"]); 
    }
    set
    {
            Session["ButtonValue"] = value;
    }
}

在同一页面的pageload上,您需要执行此操作:

protected void Page_Load(object sender, EventArgs e)  
{
    if (!IsPostBack)
    {
        YourButton.Text = ButtonTextValue;
    }
}

在第二页上你需要这样做:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "cmdBind")
    {
        LinkButton lb = (LinkButton)e.CommandSource;
        Session["ButtonValue"] = "Update";
        Response.Redirect("Form.aspx?ID=" + lb.Text + "");              
    }
}