[英]Textbox value doesn't change on button click event in asp.net C#
I'm stuck with a problem where i can't get the values of any control not just textboxes in button click event so here is the scenario, You can skip it and just can look into my button click event 我遇到了一个问题,我无法获得任何控件的值,而不仅仅是按钮点击事件中的文本框,所以这里是场景,你可以跳过它,只是可以查看我的按钮点击事件
Page product is performing 2 operations 页面产品正在执行2个操作
When a user clicks Edit on GridView in updpage It will redirects it to Product Page to update, Same page is performing Create product operation too, So when I receive QueryString value I'll update the product table and when I won't so I just perform Create operation. 当用户在更新页面中单击GridView上的编辑时,它会将其重定向到产品页面进行更新,同一页面也在执行创建产品操作,因此当我收到QueryString值时,我将更新产品表,当我不会这样时,我只是执行创建操作。
Now I'm stuck when there is no QueryString value so textboxes are updating with a new values but when there is, so they don't give me a new value. 现在,当没有QueryString值时我就陷入困境,因此文本框正在使用新值进行更新,但是当存在时,所以他们不会给我一个新值。
Here is my code 这是我的代码
On pageLoad Event I'm filling text boxes with there respective values where there is an update operation
在pageLoad事件我正在填充文本框,其中包含有更新操作的相应值
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["update"] !=null)
{
if (!Page.IsPostBack)
{
bindcategories();
bindachievments();
bindbrands();
}
int id = int.Parse(Request.QueryString["update"]);
string query = "SELECT * FROM ProductView WHERE id = " + id.ToString();
DataTable dtupd = new DataTable();
dtupd = param.All_data(query);
string name = "",available="",category="",brand="",achievement="",image="";
decimal price=0;
int unit = 0;
foreach (DataRow row in dtupd.Rows)
{
name = row.Field<string>("product_name");
price = row.Field<decimal>("price");
unit = row.Field<int>("unit");
image = row.Field<string>("product_image");
available = row.Field<string>("available");
category = row.Field<string>("category_name");
brand = row.Field<string>("brand_name");
achievement = row.Field<string>("achievement");
}
txt_name.Text = name;
txt_price.Text = price.ToString();
txt_unit.Text = unit.ToString();
product_image.ImageUrl = "../" + image;
dd_available.ClearSelection();
dd_available.SelectedValue = available;
dd_category.ClearSelection();
dd_category.Items.FindByText(category).Selected = true;
dd_brand.ClearSelection();
dd_brand.Items.FindByText(brand).Selected = true;
dd_achievment.ClearSelection();
dd_achievment.Items.FindByText(achievement).Selected = true;
btn_Insert.Text = "Update Product";
}
else
{
if (!Page.IsPostBack)
{
bindcategories();
bindbrands();
bindachievments();
}
if (!FileUpload1.HasFile)
{
product_image.ImageUrl = "../assets/images/products/default.png";
}
}
}
Button event code
按钮事件代码
protected void btn_Insert_Click(object sender, EventArgs e)
{
getpicture();
SqlCommand cmd;
string pathimage ="";
if (pathimage == "")
{
pathimage = product_image.ImageUrl;
}
else
{
pathimage = ViewState["pathimage"].ToString();
}
if (Request.QueryString["update"] != null)
{
int id = int.Parse(Request.QueryString["update"]);
string query = "UPDATE Products SET product_name=@PRODUCTNAME,price=@PRIZE,unit=@UNIT,product_image=@IMAGE,available=@AVAILABLE,product_category=@CATEGORY,product_brand=@BRAND,product_achv=@ACHIV WHERE id = @ID";
cmd = new SqlCommand(query);
txt_name.Text = "";
cmd.Parameters.Add("@PRODUCTNAME", txt_name.Text);
cmd.Parameters.Add("@PRIZE", txt_price.Text);
cmd.Parameters.Add("@UNIT", txt_unit.Text);
cmd.Parameters.Add("@IMAGE", pathimage);
cmd.Parameters.Add("@AVAILABLE", dd_available.SelectedItem.ToString());
cmd.Parameters.Add("@CATEGORY", dd_category.SelectedValue);
cmd.Parameters.Add("@BRAND", dd_brand.SelectedValue);
cmd.Parameters.Add("@ACHIV", dd_achievment.SelectedValue);
cmd.Parameters.Add("@ID", id);
param.InsertUpdateData(cmd);
}
else
{
string query = "INSERT INTO Products(product_name,price,unit,product_image,available,product_category,product_brand,product_achv) VALUES(@PRODUCTNAME,@PRIZE,@UNIT,@IMAGE,@AVAILABLE,@CATEGORY,@BRAND,@ACHIV)";
cmd = new SqlCommand(query);
cmd.Parameters.Add("@PRODUCTNAME", txt_name.Text);
cmd.Parameters.Add("@PRIZE", txt_price.Text);
cmd.Parameters.Add("@UNIT", txt_unit.Text);
cmd.Parameters.Add("@IMAGE", pathimage);
cmd.Parameters.Add("@AVAILABLE", dd_available.SelectedItem.ToString());
cmd.Parameters.Add("@CATEGORY", dd_category.SelectedValue);
cmd.Parameters.Add("@BRAND", dd_brand.SelectedValue);
cmd.Parameters.Add("@ACHIV", dd_achievment.SelectedValue);
param.InsertUpdateData(cmd);
}
}
It is getpicture function used in btn_submit
它是btn_submit中使用的getpicture函数
private void getpicture()
{
try
{
if (FileUpload1.PostedFile != null)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string root = Server.MapPath("~");
string path = root + "assets\\images\\products\\";
FileUpload1.SaveAs(path + FileName);
ViewState["pathimage"] = "/assets/images/products/" + FileName;
this.product_image.ImageUrl = "../assets/images/products/upload.png";
}
else
{
Response.Write("Select an Image");
}
}
catch (Exception ex)
{
Response.Write("Select an Image");
}
}
Providing Data access layer Insert Update Data code too
提供数据访问层也插入更新数据代码
public Boolean InsertUpdateData(SqlCommand cmd)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["OnlineStoreConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
System.Web.HttpContext.Current.Response.Write("Succed");
return true;
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.ToString());
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
Server Controls
服务器控件
<div class="row">
<div class="col-md-6">
<asp:TextBox ID="txt_name" runat="server" CssClass="form-control" placeholder="Product Name"></asp:TextBox>
<br />
<asp:TextBox ID="txt_price" runat="server" CssClass="form-control" placeholder="Product Price"></asp:TextBox>
<br />
<asp:TextBox ID="txt_unit" runat="server" CssClass="form-control" placeholder="Product Unit"></asp:TextBox>
<br />
<asp:DropDownList ID="dd_available" runat="server" CssClass="form-control">
<asp:ListItem>Is Product Available</asp:ListItem>
<asp:ListItem>Available</asp:ListItem>
<asp:ListItem>Not Avaliable</asp:ListItem>
</asp:DropDownList>
<br />
<asp:DropDownList ID="dd_category" runat="server" CssClass="form-control"></asp:DropDownList>
<br />
<asp:DropDownList ID="dd_brand" runat="server" CssClass="form-control"></asp:DropDownList>
<br />
<asp:DropDownList ID="dd_achievment" runat="server" CssClass="form-control"></asp:DropDownList>
<br />
</div>
<div class="col-md-2"></div>
<div class="col-md-4">
<asp:Image ID="product_image" runat="server" style="height:231px;width:225px;" CssClass="form-control" />
<asp:FileUpload ID="FileUpload1" runat="server" onchange = "show_image(this);" />
</div>
</div>
<br />
<div class="row">
<div class="col-md-3 col-md-offset-6">
<asp:Button ID="btn_Insert" runat="server" Text="Create Product" CssClass="btn btn-primary btn-lg" OnClick="btn_Insert_Click" />
</div>
</div>
Before Clicking update button
在单击更新按钮之前
Before clicking update button 在单击更新按钮之前
After I updated text box values respectively and when click button Upload None of my server control value changes.
我分别更新文本框值后,单击按钮上传没有我的服务器控件值更改。 Here it is
这里是
Watching local variable 看着局部变量
But in case of Creating Product it works
但是在创建产品的情况下它可以工作
Saad, i think the error is in the page_load event. Saad,我认为错误发生在page_load事件中。 If in your querystring the update param is not null, then you are always replacing the textboxes with the data of your DB.
如果在您的查询字符串中更新参数不为空,那么您始终使用数据库的数据替换文本框。 Your code should be like this
你的代码应该是这样的
protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["update"] !=null) { if (!Page.IsPostBack) { bindcategories(); bindachievments(); bindbrands(); int id = int.Parse(Request.QueryString["update"]); string query = "SELECT * FROM ProductView WHERE id = " + id.ToString(); DataTable dtupd = new DataTable(); dtupd = param.All_data(query); string name = "",available="",category="",brand="",achievement="",image=""; decimal price=0; int unit = 0; foreach (DataRow row in dtupd.Rows) { name = row.Field<string>("product_name"); price = row.Field<decimal>("price"); unit = row.Field<int>("unit"); image = row.Field<string>("product_image"); available = row.Field<string>("available"); category = row.Field<string>("category_name"); brand = row.Field<string>("brand_name"); achievement = row.Field<string>("achievement"); } txt_name.Text = name; txt_price.Text = price.ToString(); txt_unit.Text = unit.ToString(); product_image.ImageUrl = "../" + image; dd_available.ClearSelection(); dd_available.SelectedValue = available; dd_category.ClearSelection(); dd_category.Items.FindByText(category).Selected = true; dd_brand.ClearSelection(); dd_brand.Items.FindByText(brand).Selected = true; dd_achievment.ClearSelection(); dd_achievment.Items.FindByText(achievement).Selected = true; btn_Insert.Text = "Update Product"; } } else { if (!Page.IsPostBack) { bindcategories(); bindbrands(); bindachievments(); } if (!FileUpload1.HasFile) { product_image.ImageUrl = "../assets/images/products/default.png"; } } }
Hope it helps... 希望能帮助到你...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.