 |
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category.
** PLEASE BE SPECIFIC WITH YOUR QUESTION **
When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

July 28th, 2004, 02:39 PM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
help with submit button problem
Hi, I have a http table with textboxes in it. underneath it has edit, update, cancel buttons. on page_load a sub is run which fills a datareader from sqlserver tabel and the textboxes are populated with this. the problem is that when the textbox is edited and the update button is clicked the page_load runs the sub repopulateing the textboxes with the origional content before anything else can happen. So I put a hidden field in the on click for the edit button that sets a "1" in that field. then the on_load has an if then that if hidden field = 1 then it skips the sub. This works for the update button. but the cancel button doesn't work because before the cancel button code can change the hidden field to "0" the on_load is run which then skips the sub that returns data to origional.
it seems that I need an on event that fires before the page is reloaded so that it can chage it hidden field but I don't know how to do this. Any help would be greatly appreaciated.
Thanks
Raif
|

July 28th, 2004, 04:16 PM
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
in page load collect the initializations inside
if not me.IsPostBack
.........
end if
Ahmed Ali
Software Developer
|

July 28th, 2004, 05:30 PM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, I'm not sure I understand what you mean. I do have a if not page.ispostback where if it is not a postback then it populates a ddl that the client uses to choose a customer id from. if it is post back then it runs the populating sub. Maybe that is where the confustion is.
Thanks
Raif
|

July 29th, 2004, 12:48 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
if your problem is finding an event fired off befor Page_Load,the answer is Page_Init
--------------------------------------------
Mehdi.
|

July 29th, 2004, 07:44 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
You have to do as alyeng2000 says and put:
If Page.IsPostBack in the Page_Load otherwise you will lose all the values you enter when you click Update.
|

July 29th, 2004, 09:58 AM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Mehdi, I think this is close but I need some way to distinguish whether the "cancel" button has been clicked. if there was a way to set the value of a variable when the button is clicked then have the page load that would do the trick.
Thanks again
Raif
|

July 29th, 2004, 10:43 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Dear Raif, if you write your code in separate functions(better Encapsulation),you can surely decrease such runtime errors specially for working on databases in ASP.NET pages.
Hope you accept this advice and Have a look at this example.
---------
//C#
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<%@ Import namespace="System.Drawing" %>
<html>
<head>
<title>Updating Beverages</title>
</head>
<body>
<form method="post" runat="server">
<asp:Label id="lblError" runat="server" Width="164" /><br/><br/>
<asp:DataGrid id="dgProducts" runat="server"
CellPadding="5" AutoGenerateColumns="False"
OnEditCommand="EditRecord"
OnCancelCommand="CancelEdit"
OnUpdateCommand="UpdateRecord"
OnDeleteCommand="DeleteRecord">
<Columns>
<asp:BoundColumn DataField="ProductID" ReadOnly="True"
Visible="False" />
<asp:BoundColumn DataField="ProductName" ReadOnly="True"
HeaderText="Name" />
<asp:BoundColumn DataField="UnitPrice" HeaderText="Price" />
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Save" CancelText="Cancel"
EditText="Edit" />
<asp:ButtonColumn Text="Delete" CommandName="Delete" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
<script language="c#" runat="server">
private String strConnection = "server=(local);database=Northwind;integrated security=true;";
private String strSQLSelect = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID = 1";
private String ProductTableName = "ProductTable";
private SqlConnection objConnection;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
// {
// String strSQL = "INSERT INTO Products (" + "ProductName, CategoryID, UnitPrice) " + "VALUES ('RolaBolaCola', 1, 15)";
// Connect();
// SqlCommand dbComm = new SqlCommand(strSQL, objConnection);
// dbComm.ExecuteNonQuery();
// Disconnect();
LoadGrid();
// }
}
private void LoadGrid()
{
Connect();
SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
DataSet ds = new DataSet();
adapter.Fill(ds, ProductTableName);
Disconnect();
dgProducts.DataSource = ds.Tables[ProductTableName];
dgProducts.DataBind();
}
private void Connect()
{
if (objConnection == null)
objConnection = new SqlConnection(strConnection);
if (objConnection.State == ConnectionState.Closed)
objConnection.Open();
}
private void Disconnect()
{
objConnection.Close();
}
public void EditRecord(object sender, DataGridCommandEventArgs e)
{
dgProducts.EditItemIndex = e.Item.ItemIndex;
LoadGrid();
}
public void CancelEdit(object sender, DataGridCommandEventArgs e)
{
dgProducts.EditItemIndex = -1;
LoadGrid();
}
public void UpdateRecord(object sender, DataGridCommandEventArgs e)
{
// Retrieve the field values in the edited row
int ProductID = Convert.ToInt32(e.Item.Cells[0].Text);
TextBox PriceTextBox = (TextBox)e.Item.Cells[2].Controls[0];
decimal Price = Convert.ToDecimal(PriceTextBox.Text);
if (Price > 0)
{
dgProducts.EditItemIndex = -1;
lblError.Visible = false;
UpdateProduct(ProductID, Price);
}
else
{
LoadGrid();
lblError.Text = "Product price must be greater than 0!";
lblError.Visible = true;
}
// DataSet ds = new DataSet();
// dgProducts.DataSource = ds.Tables[ProductTableName];
// dgProducts.DataBind();
}
private void UpdateProduct(int ProductID, decimal Price)
{
// Create and load a DataSet with records from Northwind.Products table
Connect();
SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
DataSet ds = new DataSet();
adapter.Fill(ds, ProductTableName);
Disconnect();
// Modify the in-memory records in the DataSet
DataTable tbl = ds.Tables[ProductTableName];
tbl.PrimaryKey = new DataColumn[] {tbl.Columns["ProductID"]};
DataRow row = tbl.Rows.Find(ProductID);
row["UnitPrice"] = Price;
// Reconnect the DataSet and update the database
SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
Connect();
adapter.Update(ds, ProductTableName);
Disconnect();
}
public void DeleteRecord(object sender, DataGridCommandEventArgs e)
{
// Retrieve the ID of the product to be deleted
int ProductID = Convert.ToInt32(e.Item.Cells[0].Text);
dgProducts.EditItemIndex = -1;
DeleteProduct(ProductID);
// Display the remaining items in the DataGrid
DataSet ds = new DataSet();
dgProducts.DataSource = ds.Tables[ProductTableName];
dgProducts.DataBind();
}
private void DeleteProduct(int ProductID)
{
// Create and load a DataSet with records from Northwind's Products table
Connect();
SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
DataSet ds = new DataSet();
adapter.Fill(ds, ProductTableName);
Disconnect();
// Mark the product as Deleted in the DataSet
DataTable tbl = ds.Tables[ProductTableName];
tbl.PrimaryKey = new DataColumn[] {tbl.Columns["ProductID"]};
DataRow row = tbl.Rows.Find(ProductID);
row.Delete();
// Reconnect the DataSet and delete the record from the database
SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
Connect();
adapter.Update(ds, ProductTableName);
Disconnect();
}
</script>
--------------------
--------------------------------------------
Mehdi.
|

July 29th, 2004, 11:18 AM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the input Mehdi. I do try to have everything in it's own sub or function. Unfortunatly I'm not that familiar with c# but it looks like you are useing a datagrid. I have had sucess using datagrid in this manner. However, my current situation is not a data grid but an html table with textboxes. here is my code
Dim objConn As New SqlConnection("server = '(local)';trusted_connection = true; database = 'Method_Fitness'")
Dim ddlTXT As String
Dim ddlVal As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
updatelabel.Text = "0"
Popddl()
Else
ddlTXT = ddlclient.SelectedItem.Text
ddlVal = ddlclient.SelectedItem.Value
ddlvallabel.Text = ddlVal
ddltxtlabel.Text = ddlTXT
getClientData()
End If
End Sub
Sub Popddl()
Dim strSQL As String = "SELECT custid, namelast +', '+ namefirst as contname FROM tblCust"
Dim objAdapter As SqlDataAdapter = New SqlDataAdapter(strSQL, objConn)
Dim dsClient As DataSet = New DataSet()
objAdapter.Fill(dsClient, "tblcust")
ddlclient.DataSource = dsClient
ddlclient.DataBind()
End Sub
Function checkdbnull(ByVal input As Object)
If IsDBNull(input) Then
Return ""
Else
Return input.ToString
End If
End Function
Sub getClientData()
If updatelabel.Text = "1" Then
Exit Sub
End If
updatelabel.Text = "0"
editbutton.Enabled = "true"
cancelbutton.Enabled = "false"
updatebutton.Enabled = "false"
Dim strSql As String = "SELECT * FROM tblcust WHERE custid = '" & ddlVal & "'"
Dim ds As DataSet = New DataSet()
Dim objReader As SqlDataReader
objConn.Open()
Dim objCmd As New SqlCommand(strSql, objConn)
objReader = objCmd.ExecuteReader
If objReader.Read Then
txtlastname.ReadOnly = "true"
txtFirstname.ReadOnly = "true"
txtAddress.ReadOnly = "true"
txtcity.ReadOnly = "true"
txtst.ReadOnly = "true"
txtzip.ReadOnly = "true"
txtPhonehm.ReadOnly = "true"
txtphonewk.ReadOnly = "true"
txtphonecell.ReadOnly = "true"
txtemail.ReadOnly = "true"
txtbday.ReadOnly = "true"
txtstarted.ReadOnly = "true"
txtended.ReadOnly = "true"
txtnotes.ReadOnly = "true"
txtlastname.Text = checkdbnull(objReader("namelast"))
txtFirstname.Text = checkdbnull(objReader("namefirst"))
txtAddress.Text = checkdbnull(objReader("address"))
txtcity.Text = checkdbnull(objReader("city"))
txtst.Text = checkdbnull(objReader("state"))
txtzip.Text = checkdbnull(objReader("zip"))
txtPhonehm.Text = checkdbnull(objReader("phonehome"))
txtphonewk.Text = checkdbnull(objReader("phonework"))
txtphonecell.Text = checkdbnull(objReader("phonemobile"))
txtemail.Text = checkdbnull(objReader("email"))
txtbday.Text = checkdbnull(objReader("birthday"))
txtstarted.Text = checkdbnull(objReader("datestart"))
txtended.Text = checkdbnull(objReader("dateend"))
txtnotes.Text = checkdbnull(objReader("notes"))
objConn.Close()
End If
End Sub
Private Sub editbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editbutton.Click
cancelbutton.Enabled = "true"
updatebutton.Enabled = "true"
editbutton.Enabled = "false"
Dim strSql As String = "SELECT * FROM tblcust WHERE custid = '" & ddlVal & "'"
Dim ds As DataSet = New DataSet()
Dim objReader As SqlDataReader
objConn.Open()
Dim objCmd As New SqlCommand(strSql, objConn)
objReader = objCmd.ExecuteReader
If objReader.Read Then
txtlastname.ReadOnly = "false"
txtFirstname.ReadOnly = "false"
txtAddress.ReadOnly = "false"
txtcity.ReadOnly = "false"
txtst.ReadOnly = "false"
txtzip.ReadOnly = "false"
txtPhonehm.ReadOnly = "false"
txtphonewk.ReadOnly = "false"
txtphonecell.ReadOnly = "false"
txtemail.ReadOnly = "false"
txtbday.ReadOnly = "false"
txtstarted.ReadOnly = "false"
txtended.ReadOnly = "false"
txtnotes.ReadOnly = "false"
txtlastname.Text = checkdbnull(objReader("namelast"))
txtFirstname.Text = checkdbnull(objReader("namefirst"))
txtAddress.Text = checkdbnull(objReader("address"))
txtcity.Text = checkdbnull(objReader("city"))
txtst.Text = checkdbnull(objReader("state"))
txtzip.Text = checkdbnull(objReader("zip"))
txtPhonehm.Text = checkdbnull(objReader("phonehome"))
txtphonewk.Text = checkdbnull(objReader("phonework"))
txtphonecell.Text = checkdbnull(objReader("phonemobile"))
txtemail.Text = checkdbnull(objReader("email"))
txtbday.Text = checkdbnull(objReader("birthday"))
txtstarted.Text = checkdbnull(objReader("datestart"))
txtended.Text = checkdbnull(objReader("dateend"))
txtnotes.Text = checkdbnull(objReader("notes"))
objConn.Close()
updatelabel.Text = "1"
End If
End Sub
Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
updatelabel.Text = "0"
getClientData()
End Sub
Sub updateinfo()
updatelabel.Text = "0"
ddlvallabel.Text = ddlVal
ddltxtlabel.Text = txtlastname.Text
Dim strcustid As String = ddlVal
Dim strlastname As String = txtlastname.Text
Dim strSQL As String = "UPDATE tblcust SET namelast = '" & strlastname & "' where custid = '" & strcustid & "'"
Dim objcmd As New SqlCommand(strSQL, objConn)
objConn.Open()
objcmd.ExecuteNonQuery()
objConn.Close()
getClientData()
End Sub
Private Sub updatebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updatebutton.Click
updatelabel.Text = "0"
End Sub
End Class
Thanks again
Raif
|

July 29th, 2004, 01:46 PM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Raif,do these steps....
1-don't use hidden field.
2-make a new sub ONLY for retrieving your data from datasource and populating your textboxes ,(changing datasource should not be included here),suppose the name is SHOWDATA()
3-make a new sub that ONLY updates(changes) your datasource(like updateinfo()) (showing the data or anyother things should not be included)it gets values from your textboxes and validates them and executes queries for updating your datasource according to your validated values of textboxes, suppose the name is
UPDATEDATA()
4-change updatebutton_Click
Private Sub updatebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updatebutton.Click
//....
UPDATEDATA()
SHOWDATA()
End Sub
5-change cancel_Click
Private Sub cancelbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelbutton.Click
//....
SHOWDATA()
End Sub
6-change Edit_Click
Private Sub Editbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelbutton.Click
//....
SHOWDATA()
//make the texboxes ready for updating your datasource
End Sub
7-change PageLoad()
If Not Page.IsPostBack Then
//....
SHOWDATA()
Else
//.....
//don't put here any code that shows or changes the datasource
End If
Raif ,go on this,you will surely achieve your goal.....
HTH.
--------------------------------------------
Mehdi.
|

July 29th, 2004, 03:31 PM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Mehdi. I'm definately closer now. two problems. One the cancel button inexplicably still doesn't do anything. all I have under cancel click is as follows
Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
getClientData()
End Sub
There is not reason at all for it to do nothing!
Second, I need to have a dropdownlist that allows client to choose which customer they are editing. here is my page load
If Not Page.IsPostBack Then
Popddl()
ddlVal = ddlclient.SelectedItem.Value
ddlTXT = ddlclient.SelectedItem.Text
getClientData()
Else
ddlTXT = ddlclient.SelectedItem.Text
ddlVal = ddlclient.SelectedItem.Value
End If
so it populates the ddl but when I try to choose a different record it doesn't change the text fields. However when I click the edit button it changes the textboxes to the record that I selected.
Your sugections really helped clean up my code but I'm still not quite there. Any insights are greatly appreaciated
Thanks
Raif
|
|
 |