So I am new to ASP.NET C# and having trouble in creating a cancel function. When the cancel function click it supposedly discard all the changes the user made inside the inputfield/dropdown and return to it's previous value before the modification has made. Is there any way to this kind of function? thanks in advance.
As you can see in the photo, the user has made some modification in the selected row in gridview and when the click the cancel button, the changes should be disregarded and turn back to it's previous value .
Inputbo/dropdown part
<table class="style2" >
<tr>
<td class="style3" >Department Case #</td>
<td> <asp:TextBox ID="TextBox1" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
</tr>
<tr>
<td class="style3">Department</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server"
Height="18px" Width="153px" Enabled="False" AppendDataBoundItems="true"
AutoPostBack="true" DataSourceID="SqlDataSource2"
DataTextField="DEPARTMENT_NAME" DataValueField="DEPARTMENT_CODE"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Text="--- Select ----" Value=" " />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:*****ConnectionString %>"
SelectCommand="SELECT [DEPARTMENT_CODE], [DEPARTMENT_NAME] FROM [TV_DEPTNAME]">
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="style3">Charge</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server"
Height="22px" Width="153px" Enabled="False" AppendDataBoundItems="true"
AutoPostBack="true" DataSourceID="SqlDataSource3"
DataTextField="OFFENSE_DESCRIPTION" DataValueField="OFFENSE_CODE"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Text="--- Select ----" Value=" " />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:****ConnectionString %>"
SelectCommand="SELECT [OFFENSE_CODE], [OFFENSE_DESCRIPTION] FROM [TV_OFFENSE]">
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="style3">Lab Case #</td>
<td><asp:TextBox ID="TextBox4" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
</tr>
<tr>
<td class="style3">Incident Report Date</td>
<td><asp:TextBox ID="TextBox5" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
</tr>
</table>
button part
<asp:TextBox ID="TextBox6" runat="server" Visible="False"></asp:TextBox>
<br />
<asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" Enabled="false"/>
<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click" Text="Cancel" Enabled="false"/>
<br />
CODE BEHIND
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ ///<summary> Change the mouse cursor to Hand symbol to show the user the cell is selectable</summary>
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
///<summary> Attach the click event to each cells</summary>
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Select")
{
///<summary>
///Convert the row index stored in the CommandArgument
///property to an Integer.
///</summary>
LoadData(Convert.ToInt32(e.CommandArgument));
}
}
private void LoadData(int? rowNumber = null)
{
//if rowNumber is null use GridView1.SelectedIndex
var index = rowNumber ?? GridView1.SelectedIndex;
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = GridView1.Rows[index];
//Populate the input box with the value of selected row.
GridViewRow gr = GridView1.Rows[index];
TextBox1.Text = gr.Cells[2].Text;
TextBox7.Text = gr.Cells[3].Text;
TextBox8.Text = gr.Cells[4].Text;
TextBox4.Text = gr.Cells[5].Text;
TextBox5.Text = gr.Cells[6].Text;
TextBox6.Text = gr.Cells[1].Text;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{ // to set the value of the selected item in dropdown
DropDownList1.SelectedValue = DropDownList1.SelectedItem.Value;
DropDownList2.SelectedValue = DropDownList2.SelectedItem.Value;
}
protected void btnEdit_Click(object sender, EventArgs e)
{ ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
SetEnable(true);
}
protected void btnSave_Click(object sender, EventArgs e)
{ ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
SetEnable(false);
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source=******\MSSQL****;Initial Catalog=*****;User ID=****;Password=****";
cnn = new SqlConnection(connetionString);
cnn.Open();
SqlCommand cmd = new SqlCommand("Update TV_LABCASE Set DEPARTMENT_CASE_NUMBER=@DEPARTMENT_CASE_NUMBER,DEPARTMENT_CODE=@DEPARTMENT_NAME,OFFENSE_CODE=@OFFENSE_DESCRIPTION,LAB_CASE=@LAB_CASE,OFFENSE_DATE=@OFFENSE_DATE where CASE_KEY=@CASE_KEY", cnn);
cmd.Parameters.AddWithValue("@DEPARTMENT_CASE_NUMBER", TextBox1.Text);
cmd.Parameters.AddWithValue("@DEPARTMENT_NAME", DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@OFFENSE_DESCRIPTION", DropDownList2.SelectedValue);
cmd.Parameters.AddWithValue("@LAB_CASE", TextBox4.Text);
cmd.Parameters.AddWithValue("@OFFENSE_DATE", TextBox5.Text);
cmd.Parameters.AddWithValue("@CASE_KEY", TextBox6.Text);
cmd.ExecuteNonQuery();
cnn.Close();
TextBox7.Text = DropDownList1.SelectedItem.Text;
TextBox8.Text = DropDownList2.SelectedItem.Text;
GridView1.DataBind();
}
protected void btnCancel_Click(object sender, EventArgs e)
{ ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
SetEnable(false);
LoadData();
}
///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
private void SetEnable(bool editable)
{
btnEdit.Enabled = !editable;
btnSave.Enabled = editable;
btnCancel.Enabled = editable;
TextBox1.Enabled = editable;
DropDownList1.Enabled = editable;
DropDownList2.Enabled = editable;
TextBox4.Enabled = editable;
TextBox5.Enabled = editable;
TextBox7.Visible = !editable;
TextBox8.Visible = !editable;
DropDownList1.Visible = editable;
DropDownList2.Visible = editable;
}
i'll just explained a while ago that i also have some issues in displaying the selected row value inside the dropdown, so instead of displaying inside the dropdown, I decided to put the value inside the textbox (which is why I included textbox 7 and 8 and they are only use to display the value of the selected row, after clicking edit button, it will be hidden and the dropdown will then be visible thus making effect of textbox turning into dropdown ) and hide and show them if i needed them. now the problem is that after I use the code you have given, the changes inside the textboxes wont save after clicking save button.