按下提交按钮后,sql查询无法正常工作

时间:2014-03-28 11:44:28

标签: c# asp.net sql ajax

我写过弹出式模态代码。

第一个,效果很好,第二个在单击提交按钮时不起作用。有什么区别?我想使用第二个代码。 (唯一不同的是,使用“ScriptManager”运行的代码和不能使用“ToolkitScriptManager”运行的代码)。我怎么能设法做到这一点?

第一个,提交作品

<div>
        <asp:ScriptManager ID="sm" runat="server">
        </asp:ScriptManager>
        <asp:Panel ID="pnlModal" runat="server" BorderColor="AliceBlue" BorderStyle="Solid"
            BorderWidth="2px" Width="300px" Height="100px">
            <b>שם מוצר:</b>
            <asp:TextBox ID="nameProductTB" runat="server"></asp:TextBox><br />
            <asp:RequiredFieldValidator ControlToValidate="nameProductTB" CssClass="reqVld" ID="nameProductTB_req_vld"
                runat="server" ErrorMessage="שדה חובה"></asp:RequiredFieldValidator>
            <b>שם היצרן:</b>
            <asp:TextBox ID="manufacturerProductTB" runat="server"></asp:TextBox><br />
            <b>תמונה:</b>
            <asp:FileUpload ID="uploadImageProductFU" runat="server"></asp:FileUpload><br />
            <asp:RequiredFieldValidator ControlToValidate="uploadImageProductFU" ID="uploadImageProductFUVLD"
                runat="server" ErrorMessage="שדה חובה" CssClass="reqVld"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="uploadImageProductFUImgVLD" runat="server" ControlToValidate="uploadImageProductFU"
                ValidationExpression="^([0-9a-zA-Z_\-~ :\\])+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.gif|.GIF|.png|.PNG)$"
                Text="נא העלה תמונה" />
            <asp:Button ID="SubmitNewProduct" runat="server" Text="העלה רכיב חדש" OnClick="SubmitNewProduct_Click"
                CausesValidation="False"></asp:Button>
        </asp:Panel>
        <asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="RadioButtonProduct"
            PopupControlID="pnlModal" OkControlID="SubmitNewProduct">
        </asp:ModalPopupExtender>
    </div>

第二,提交无效

<div>
            <asp:ToolkitScriptManager ID="TKSMnewProduct" runat="server">
    </asp:ToolkitScriptManager>
    <asp:Panel ID="pnlModal" runat="server" BorderColor="AliceBlue" BorderStyle="Solid"
        BorderWidth="2px" Width="300px" Height="100px">
        <b>שם מוצר:</b>
        <asp:TextBox ID="nameProductTB" runat="server"></asp:TextBox><br />
        <asp:RequiredFieldValidator ControlToValidate="nameProductTB" CssClass="reqVld" ID="nameProductTB_req_vld"
            runat="server" ErrorMessage="שדה חובה"></asp:RequiredFieldValidator>
        <b>שם היצרן:</b>
        <asp:TextBox ID="manufacturerProductTB" runat="server"></asp:TextBox><br />
        <b>תמונה:</b>
        <asp:FileUpload ID="uploadImageProductFU" runat="server"></asp:FileUpload><br />
        <asp:RequiredFieldValidator ControlToValidate="uploadImageProductFU" ID="uploadImageProductFUVLD"
            runat="server" ErrorMessage="שדה חובה" CssClass="reqVld"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="uploadImageProductFUImgVLD" runat="server" ControlToValidate="uploadImageProductFU"
            ValidationExpression="^([0-9a-zA-Z_\-~ :\\])+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.gif|.GIF|.png|.PNG)$"
            Text="נא העלה תמונה" />
        <asp:Button ID="SubmitNewProduct" runat="server" Text="העלה רכיב חדש" OnClick="SubmitNewProduct_Click"
            CausesValidation="False"></asp:Button>
    </asp:Panel>
    <asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="RadioButtonProduct"
        PopupControlID="pnlModal" OkControlID="SubmitNewProduct">
    </asp:ModalPopupExtender>
</div>

返回代码

protected void SubmitNewProduct_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
    if (uploadImageProductFU.HasFile)
    {
        string productName = nameProductTB.Text;
        string str = uploadImageProductFU.FileName;
        string fname = null;
        string path = Server.MapPath(".") + "\\uploads\\" + productName;
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        try
        {
            fname = path + "\\" + uploadImageProductFU.FileName;
            uploadImageProductFU.SaveAs(fname);
            string UserImageUrl = "~\\uploads\\" + productName + "\\" + uploadImageProductFU.FileName;
        }
        catch
        {
            //return;
        }
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into TblProduct (ImageURL, Manufacturer, Name, AverageGrade) values ('" + fname + "', @Manufacturer,'" + productName + "', 0)", con);
        cmd.Parameters.AddWithValue("@Manufacturer", manufacturerProductTB.Text);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

0 个答案:

没有答案