Submit button to update database, won't work if there is postback stated in page_load RRS feed

  • Question

  • User-1668697385 posted

    I have a web form page that I am going to query the data from database via selecting different values from dropdown list, after I selected proper value, in the description I want to update the database record while I click the submit button, for example, customer name, and status, there will be only one record coming back and the description will show(I can do it in `isPostback` and query the database, using SQL DataReader and then call related index element to get it) but when I typed something in the description and click submit, it won't update in the database, but if I don't use `if(isPostback)` it is working.
    P.S. if there is no `if(ispostback)` case block there,`dataUpdate.Update()` works well.
    So my question is;


    [1]: https://i.stack.imgur.com/ylfTi.png

    protected void btnSubmit_Click(object sender, EventArgs e)

    The page_load code:

    protected void Page_Load(object sender, EventArgs e)
    ValidationSettings.UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;

    if (!IsPostBack)
    SqlConnection conn1 = new SqlConnection(cxInfo.ConnectionString);
    string userInfoQuery1 = "select * from users where id=@id";
    SqlCommand userInfo1 = new SqlCommand(userInfoQuery1, conn1);
    userInfo1.Parameters.AddWithValue("@id", dropdlCx.SelectedValue);
    SqlDataReader reader1 = userInfo1.ExecuteReader();
    if (reader1.HasRows )
    lblCxId.Text = "" + reader1[0];
    SqlConnection conn = new SqlConnection(dataUpdate.ConnectionString);
    string userInfoQuery = "select cx_first_name+',' + cx_last_name as 'name',cx_id ,incident_id,description,contact_method from incident where cx_id=@id and status=@status";
    SqlCommand userInfo = new SqlCommand(userInfoQuery, conn);
    userInfo.Parameters.AddWithValue("@id", lblCxId.Text);
    userInfo.Parameters.AddWithValue("@status", dropStatus.SelectedValue);
    SqlDataReader reader = userInfo.ExecuteReader();
    if (reader.HasRows)
    lblCxId.Text = "" + reader1[0];
    txtDesc.Text = (string)reader[3];
    radioContact.SelectedValue = (string)reader[4];
    desc= (string)reader[3];


    if (IsPostBack)
    lblCxId.Text = dropdlCx.SelectedValue;
    /*I want to have txtDesc.txt= database query here, but as long as I add the code here, then the update won't work*/

    So I think there only thing I got stuck is how to let the system know that what I submit is not the thing that he is going to refresh, please accept my value instead of the refresh?

    Sunday, March 10, 2019 6:21 AM

All replies

  • User475983607 posted

    P.S. if there is no `if(ispostback)` case block there,`dataUpdate.Update()` works well.

    You are struggling with ASP.NET Web Forms fundamental programming patterns.  The IsPostBack() method is used to determine if we need to process user input or this is the first time the page loaded.  Usually, the first time the page loads we need to initialize the page members.

    Probably the best course of action is to go through a few tutorials to learn the basics.



    Sunday, March 10, 2019 1:21 PM
  • User-893317190 posted

    Hi DavidShi,

    From dataUpdate.Update();, it seems that you are using a Sqldatasource.  

    You say  I want to have txtDesc.txt= database query here, but as long as I add the code here, then the update won't work,

    How does you sqldatasource work? Could you provide the make up of your aspx?

    When you click the submit button , your code in IsPostBack will execute and then your code in the btnSubmit_Click.

    Does the code in  your ispostback will cause dataUpdate.Update();  to fail?

    Dose server shows any error message?

    Please provide more information about your problem.

    Best regards,

    Ackerly Xu

    Monday, March 11, 2019 4:26 AM
  • User-1668697385 posted

    Hi Ackerly,

    Thank you for your answer, My only problem now is :

    if I select  a customer name, and a incident name(this one will read the customer ID and return related incident_id, I still want to get reportdate, description and contact_method from database queries, but the thing is If I code database queries in if(ispostback) case, the system will always load the database and won't let me update something in description and contact_method. if I code it in if(!isvalid) block, still won't make it work, as the two drop down list is set with autopostback=true, every time I select anything, it will be autopostback which will set ispostback=true.

    there is no error there, there is only nothing coming out, neither expected nor unexpected.Page

    Tuesday, March 12, 2019 7:39 PM
  • User-893317190 posted

    Hi DavidShi,

    It seems that  you haven done initial binding in !IsPostback.

    And when the user choose a select  option, it will cause autopostback  and when autopostback you want to update  the content of description , Report Date and Time and Method of Contact , right?

    Generally speaking , if you want to deal with the autopostback event of a dropdownlist, you had better not write it in both ispostback and !ispostback, you should write your code in the dropdownlist's selectedIndexChange event.

    About selectedIndexChange event, you could refer to the link below 


    Best regards,

    Ackerly Xu

    Wednesday, March 13, 2019 1:13 AM