views:

290

answers:

2

Hello all,

Thanks in advance for your help.

I am using c#.net.

I have two views on my webpage (contained within one multiview), both contain buttons.

view_1

Contains a repeater/datasource and an custom made ‘edit’ button (which holds the ID for each row returned).

view_2

Contain an ‘update’ form and a ‘update’ button. When the user presses the update button the information within the database for that particular row is updated.

The problem I believe lies with my ‘update’ button within view_2

Code behind (‘update’ button), I have an if statement:

        protected void Page_Load(object sender, EventArgs e)
    {
        updateSuccessFailed.Visible = false;

        if (!Page.IsPostBack)
        {
            _multiView1.ActiveViewIndex = 0;
        }
    }   

protected void update_Click(object sender, EventArgs e)

{

    var Id = Convert.ToInt32((ID.Value));

    notYetUpdated.Visible = true;
    updateSuccessFailed.Visible = false;

     tblV updateV = new tblV();

        updateV.vID = venueId;
        updateV.vame = updateName.ToString();
        updateV.vPostcode = updatePropPostcode.ToString();

        if (vRepos.Update(updateV))
        {
            notYetUpdated.Visible = false;
            updateSuccessFailed.Visible = true;
            updateMessage.Text = "Updated.";
        }
        else
        {
            notYetUpdated.Visible = false;
            updateSuccessFailed.Visible = true;
            updateMessage.Text = "An error has occurred, please try again.";
        }
}

_view2

<asp:View ID="_view2" runat="server">      
    <div style="text-align:center" runat="server" id="notYetUpdated">
      <table border="0" cellspacing="1">
        <tr>
          <td style="text-align:left;">Name</td>
          <td style="text-align:left;"><asp:TextBox ID="updateName" MaxLength="60" runat="server" /></td>
        </tr>
        <tr>
          <td style="text-align:left;">Postcode</td>
          <td style="text-align:left;"><asp:TextBox ID="updatePropPostcode" MaxLength="60" runat="server" /></td>         
        </tr>         
      </table><br />              

      <asp:Button ID="updateVCancel" Text="Cancel" runat="server" onclick="cancel_Click" CssClass="standardButton" />
      <asp:Button ID="updateVConfirm" Text="Update" runat="server" onclick="update_Click" CssClass="standardButton" />
      <asp:HiddenField ID="vUpdateID" runat="server" />     
    </div> 

    <div style="text-align:center" runat="server" id="updateSuccessFailed">
      <p><asp:Label ID="updateMessage" runat="server" /></p>
      <asp:Button ID="updateBack" Text="Back To Start" runat="server" onclick="backToStart_Click" CssClass="standardButton" /> 
    </div>

  </asp:View>

notYetUpdated / updateSuccessFailed are div’s which hold different information.

When the user first ‘updates’ a record it make the right div visible. (notYetUpdated – holds the form information / updateSuccessFailed – holds a message to state whether the record has been updated or not). However when you access the view_2 again it accesses the update_Click event and updateSuccessFailed is visible even though it shouldn’t be.

I thought I could clear all stored information within the viewstates with the code below, however this is not working.

    ViewState.Clear();
    ClearChildViewState();

Thanks

Clare :-)

A: 

The 4th line should be updateSuccessFail**ed**.Visible = false;?

RioTera
Sorry misread your message. That was a repo when writing out this question. Withih my code is a fail**ed**.
ClareBear
Clare, if I understood the problem I think you should set the updateSuccessFailed.Visible to false out of the update_Click function. For example in Page_Load
RioTera
I have added updateSuccessFailed.Visible = false within my Page_Load, however the second time a user enters view_2 nothing is displayed. Can it be because the button been pressed is somehow being cached?
ClareBear
It's difficult to answer without the rest of code
RioTera
I have added more code, is this any better? I believe what is happening is when I press the 'back to start’ button located within the updateSuccessFailed div. It is somehow storing that the 'update' button has been pressed. Therefore when I access the 'edit' button for another row (via the _view1) it passes me through to _view2, however it already thinks that the 'update' button has been pressed so goes straight into the update_clicked event.
ClareBear
Try to add EnableViewState="False" to updateVConfirm button<asp:Button ID="updateVConfirm" Text="Update" runat="server" onclick="update_Click" CssClass="standardButton" EnableViewState="False" />
RioTera
A: 

This was an error on my part. I adapted my code, here it is:

var Id = Convert.ToInt32((ID.Value)); 

tblV updateV = new tblV();        
updateV.vID = venueId;        
updateV.vame = updateName.ToString();        
updateV.vPostcode = updatePropPostcode.ToString();  

notYetUpdated.Visible = false;    
updateSuccessFailed.Visible = true; 

if (vRepos.Update(updateV))        
{                   
updateMessage.Text = "Updated.";        
}        
else        
{     
updateMessage.Text = "An error has occurred, please try again.";        
}

Hope this helps other people.

ClareBear