为什么Asp:GridView在服务器端调用按钮单击方法?

时间:2016-07-30 05:30:13

标签: c# asp.net gridview

在我的情况下,我asp:gridview使用OnRowCommand调用服务器方法,但是当我尝试手动刷新页面时,它也会调用OnRowCommand方法,我不知道在哪里是实际问题。

我的代码:.aspx

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="CUG_OnSelectedIndexChanged" OnRowCommand="CugSubmit_onserverclick1"  EnableViewState="true"BorderStyle="Solid" BorderColor="Gray" BorderWidth="1px" EditRowStyle-BorderStyle="Solid" EditRowStyle-BorderWidth="1px" EditRowStyle-BorderColor="Gray"  HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1px" HeaderStyle-BorderColor="Gray" RowStyle-BorderStyle="Solid" RowStyle-BorderColor="Gray"  RowStyle-BorderWidth="1px" EmptyDataRowStyle-BorderStyle="Solid" EmptyDataRowStyle-BorderColor="Black" EmptyDataRowStyle-BorderWidth="1px"  SortedAscendingHeaderStyle-BorderStyle="Solid" SortedAscendingHeaderStyle-BorderColor="Black" SortedAscendingHeaderStyle-BorderWidth="1px">
    <Columns>
    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
    <asp:TemplateField HeaderText="Driver ID">
        <ItemTemplate>
        <asp:Label ID="lbl_Dri_Name" runat="server" Text='<%# Eval("VehicleNo") %>' Visible="false"></asp:Label>
            <asp:TextBox ID="Dri_Name" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Dedicated">
                <ItemTemplate>
        <asp:Label ID="lbl_Vehitype" runat="server" Text='<%# Eval("VehicleType") %>' Visible="false"></asp:Label>
                    <asp:CheckBox ID="Vehitype" runat="server" AutoPostBack="true" />
                </ItemTemplate>
        </asp:TemplateField>

    <asp:ButtonField CommandName="ProcessThis" Text="Submit" />
    <asp:ButtonField CommandName="Select" ItemStyle-Width="30" Text="delete" HeaderText="Delete" />
    <asp:TemplateField HeaderText="">
        <ItemTemplate>

       </ItemTemplate>
        <FooterStyle HorizontalAlign="left" />
        <FooterTemplate>
         <asp:Button ID="ButtonAdd" runat="server" Text="Add Row" onclick="ButtonAdd_Click" />
        </FooterTemplate>
    </asp:TemplateField>
    </Columns></asp:gridview>

我的代码:.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetCUGList();
        }
    }
protected void CugSubmit_onserverclick1(object sender, GridViewCommandEventArgs e)
    {
        DL.CustomerProfile ObjInput = new CustomerProfile();
        BL.CustomerInputBL ObjCustomerInputBL = new CustomerInputBL();

        if (e.CommandName == "ProcessThis")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = Gridview1.Rows[index];

            if (row.RowType == DataControlRowType.DataRow)
            {
                TextBox textBox = row.FindControl("Dri_Name") as TextBox;
                CheckBox chk = row.FindControl("Vehitype") as CheckBox;

                ObjInput.CustId = HttpContext.Current.Session["VidStr"].ToString();
                ObjInput.CustName = HttpContext.Current.Session["FirstName"].ToString();

                ObjInput.VehiNo = textBox.Text;
                ObjInput.Update = "Insert";
                if (chk.Checked == true)
                {
                    ObjInput.VehiChk = 1;
                }

                else
                {
                    ObjInput.VehiChk = 0;
                }

                string URL = "http://*****/CustomerServices.svc/CUGList/";
                string OutStatus = ObjCustomerInputBL.GetConnection(ObjInput, URL);
                var serializer = new JavaScriptSerializer();
                //AllTypeDetails.Value = OutStatus;
                DataSet data = JsonConvert.DeserializeObject<DataSet>(OutStatus);
                string ds = data.Tables[0].Rows[0]["ErrorMessage"].ToString();
                if (ds == "already exist")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number has already registered')", true);
                    GetCUGList();
                }

                else if (ds == "Not Use")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number is not in freightX')", true);
                    GetCUGList();
                }

                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Insert Request Sent to Admin')", true);



                    GetCUGList();
                }
            }
            AddNewRowToGrid();
        }


        else if (e.CommandName == "ProcessThisUpdate")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = Gridview1.Rows[index];

            if (row.RowType == DataControlRowType.DataRow)
            {
                TextBox textBox = row.FindControl("Dri_Name") as TextBox;
                CheckBox chk = row.FindControl("Vehitype") as CheckBox;

                ObjInput.CustId = HttpContext.Current.Session["VidStr"].ToString();
                ObjInput.CustName = HttpContext.Current.Session["FirstName"].ToString();
                ObjInput.Update = "Update";
                ObjInput.VehiNo = textBox.Text;

                if (chk.Checked == true)
                {
                    ObjInput.VehiChk = 1;
                }

                else
                {
                    ObjInput.VehiChk = 0;
                }

                string URL = "http://*******/CustomerServices.svc/CUGList/";
                string OutStatus = ObjCustomerInputBL.GetConnection(ObjInput, URL);
                var serializer = new JavaScriptSerializer();
                //AllTypeDetails.Value = OutStatus;
                DataSet data = JsonConvert.DeserializeObject<DataSet>(OutStatus);
                string ds = data.Tables[0].Rows[0]["ErrorMessage"].ToString();
                if (ds == "already exist")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number has already registered')", true);
                    GetCUGList();
                }

                else if (ds == "Not Use")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number is not in freightX')", true);
                    GetCUGList();
                }

                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Update Request Sent to Admin')", true);
                    GetCUGList();
                }
            }
        }

    }

0 个答案:

没有答案