Newbie: capturing changed value of text field instead of default

G

Guest

On Page_Load I have set a default value to the field TxtDate.Text as shown
below:
TxtDate.Text = DateTime.Now.ToShortTimeString();

On the Click Event of my BtnSave I want to capture the default value if the
user hasn't changed it, or the edited value of the field if the user has
changed it.

The code below works correctly with the only exception being that I cannot
capture changes to that TxtDate.Text field. Any thoughts would be welcome.

Thanks,

Samantha

Code Behind:

private void BtnSave_Click(object sender, System.EventArgs e)
{

LblError.Visible = true;
SqlConnection sqlCon6 = null;

try
{
sqlCon6 = GetConnection();
SqlCommand sqlAddLog = new SqlCommand ("sqlAddLog", sqlCon6);
sqlCon6.Open();
sqlAddLog.CommandType = CommandType.StoredProcedure;

foreach (ListItem li in LstBoxServers.Items)
{
if (li.Selected == true)
{
sqlAddLog.Parameters.Clear();

sqlAddLog.Parameters.Add("@Admin_Id",System.Data.SqlDbType.Int,4,
"Admin_Id").Value = ddlAdmins.SelectedItem.Value;
sqlAddLog.Parameters.Add("@Entry_Date_Time",System.Data.SqlDbType.DateTime, 8, "Entry_Date_Time").Value = Calendar1.SelectedDate;
sqlAddLog.Parameters.Add("@Comments",System.Data.SqlDbType.VarChar,5000, "Comments").Value = TxtAction.Text;
sqlAddLog.Parameters.Add("@Server_Id",System.Data.SqlDbType.Int, 4,
"Server_Id").Value = li.Value;
sqlAddLog.Parameters.Add("@Entry_Time",System.Data.SqlDbType.VarChar,
50, "Entry_Time").Value = TxtDate.Text;

sqlAddLog.ExecuteNonQuery();

}
}
}


catch ( System.Exception Ex )
{
Console.WriteLine();

LblError.Text = Ex.Message;
}

finally
{
if (sqlCon6 != null)
sqlCon6.Close();
Response.Redirect ("ControlPanel.aspx");
}

}


And the HTML...

<%@ Page language="c#" Codebehind="CreateLog.aspx.cs"
AutoEventWireup="false" Inherits="ServerLogBook.CreateLog" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CreateLog</title>
<meta content="False" name="vs_snapToGrid">
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:button id="BtnCancel" style="Z-INDEX: 102; LEFT: 565px; POSITION:
absolute; TOP: 330px"
runat="server" Text="Cancel" Height="24px" Width="96px"
CausesValidation="False"></asp:button><asp:table id="TblHeader"
style="Z-INDEX: 118; LEFT: 272px; POSITION: absolute; TOP: 8px" runat="server"
Width="584px">
<asp:TableRow VerticalAlign="Bottom" HorizontalAlign="Center"
Font-Names="Arial" Font-Bold="True">
<asp:TableCell Text="Information Services"></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell VerticalAlign="Bottom" Font-Names="Arial"
Font-Bold="True" HorizontalAlign="Center"
Text="Server Log Book"></asp:TableCell>
</asp:TableRow>
</asp:table><asp:button id="BtnView" style="Z-INDEX: 117; LEFT: 465px;
POSITION: absolute; TOP: 64px" runat="server"
Text="View" Height="24px" Width="96px"
CausesValidation="False"></asp:button><asp:button id="BtnCreate"
style="Z-INDEX: 106; LEFT: 565px; POSITION: absolute; TOP: 64px"
runat="server" Text="Create" Height="24px" Width="104px"
CausesValidation="False"></asp:button><asp:textbox id="TxtNewAdmin"
style="Z-INDEX: 101; LEFT: 464px; POSITION: absolute; TOP: 120px"
runat="server" Width="160px"></asp:textbox><asp:imagebutton
id="ImgBtnNewAdmin" style="Z-INDEX: 116; LEFT: 504px; POSITION: absolute;
TOP: 104px"
runat="server" ImageUrl="file:///C:\Program Files\Microsoft Visual
Studio .NET 2003\Common7\Graphics\bitmaps\Outline\PLUS.BMP"
CausesValidation="False"></asp:imagebutton><asp:imagebutton
id="ImgBtnAddSign" style="Z-INDEX: 115; LEFT: 752px; POSITION: absolute; TOP:
104px"
runat="server" ImageUrl="file:///C:\Program Files\Microsoft Visual
Studio .NET 2003\Common7\Graphics\bitmaps\Outline\PLUS.BMP"
CausesValidation="False"></asp:imagebutton><asp:button id="BtnSave"
style="Z-INDEX: 103; LEFT: 465px; POSITION: absolute; TOP: 330px"
runat="server"
Text="Save" Height="24px" Width="96px"></asp:button><asp:textbox
id="TxtAction" style="Z-INDEX: 104; LEFT: 465px; POSITION: absolute; TOP:
226px"
runat="server" Height="88px" Width="400px"
TextMode="MultiLine"></asp:textbox><asp:label id="LblError" style="Z-INDEX:
105; LEFT: 254px; POSITION: absolute; TOP: 370px"
runat="server" Width="607px" Font-Size="X-Small"></asp:label><asp:label
id="LblServerName" style="Z-INDEX: 107; LEFT: 672px; POSITION: absolute; TOP:
104px"
runat="server" Height="8px" Width="144px" Font-Names="Arial"
Font-Size="X-Small">Server Name</asp:label><asp:textbox id="TxtNewServerName"
style="Z-INDEX: 108; LEFT: 672px; POSITION: absolute; TOP: 120px"
runat="server" Width="160px"></asp:textbox><asp:dropdownlist
id="ddlAdmins" style="Z-INDEX: 119; LEFT: 464px; POSITION: absolute; TOP:
120px"
runat="server" Width="168px"></asp:dropdownlist><asp:label
id="LblUserName" style="Z-INDEX: 109; LEFT: 464px; POSITION: absolute; TOP:
104px"
runat="server" Height="8px" Width="144px" Font-Names="Arial"
Font-Size="X-Small">Admin</asp:label><asp:textbox id="TxtDate"
style="Z-INDEX: 110; LEFT: 246px; POSITION: absolute; TOP: 287px"
runat="server"
Width="160px"></asp:textbox><asp:label id="LblDate" style="Z-INDEX: 111;
LEFT: 246px; POSITION: absolute; TOP: 271px" runat="server"
Height="16px" Width="152px" Font-Names="Arial"
Font-Size="X-Small">Time</asp:label><asp:label id="LblAction" style="Z-INDEX:
112; LEFT: 464px; POSITION: absolute; TOP: 210px"
runat="server" Height="8px" Width="393px" Font-Names="Arial"
Font-Size="X-Small">Action</asp:label><asp:imagebutton id="ImgBtnSaveServer"
style="Z-INDEX: 113; LEFT: 839px; POSITION: absolute; TOP: 125px"
runat="server" Height="18px" Width="18px" ImageUrl="file:///C:\Program
Files\Microsoft Visual Studio .NET
2003\Common7\Graphics\bitmaps\OffCtlBr\Large\Color\SAVE.BMP"
CausesValidation="False"></asp:imagebutton><asp:imagebutton
id="ImgBtnSaveAdmin" style="Z-INDEX: 114; LEFT: 639px; POSITION: absolute;
TOP: 125px"
runat="server" Height="18px" Width="18px" ImageUrl="file:///C:\Program
Files\Microsoft Visual Studio .NET
2003\Common7\Graphics\bitmaps\OffCtlBr\Large\Color\SAVE.BMP"
CausesValidation="False"></asp:imagebutton><asp:calendar id="Calendar1"
style="Z-INDEX: 120; LEFT: 246px; POSITION: absolute; TOP: 120px"
runat="server" Height="144px" Width="208px" Font-Names="Verdana"
Font-Size="9pt" CellPadding="0" BorderColor="White" ForeColor="Black"
BackColor="White" NextPrevFormat="ShortMonth" BorderWidth="1px">
<TodayDayStyle BackColor="#CCCCCC"></TodayDayStyle>
<NextPrevStyle Font-Size="8pt" Font-Bold="True" ForeColor="#333333"
VerticalAlign="Bottom"></NextPrevStyle>
<DayHeaderStyle Font-Size="8pt" Font-Bold="True"></DayHeaderStyle>
<SelectedDayStyle ForeColor="White"
BackColor="#333399"></SelectedDayStyle>
<TitleStyle Font-Size="12pt" Font-Bold="True" BorderWidth="4px"
ForeColor="#333399" BorderColor="Black"
BackColor="White"></TitleStyle>
<OtherMonthDayStyle ForeColor="#999999"></OtherMonthDayStyle>
</asp:calendar><asp:listbox id="LstBoxServers" style="Z-INDEX: 121; LEFT:
672px; POSITION: absolute; TOP: 120px"
runat="server" Height="73px" Width="163px"
SelectionMode="Multiple"></asp:listbox>
<asp:RequiredFieldValidator id="rfvddlAdmins" style="Z-INDEX: 122; LEFT:
472px; POSITION: absolute; TOP: 152px"
runat="server" Font-Size="Smaller" Font-Names="Arial Narrow"
InitialValue="--Select an Admin--" ControlToValidate="ddlAdmins"
ErrorMessage="The Admin field is required"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="rfvTxtAction" style="Z-INDEX: 123; LEFT:
693px; POSITION: absolute; TOP: 329px"
runat="server" Width="146px" Font-Size="Smaller" Font-Names="Arial
Narrow" ControlToValidate="TxtAction"
ErrorMessage="The Action field is required"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="rfvLstBoxServers" style="Z-INDEX: 124;
LEFT: 675px; POSITION: absolute; TOP: 70px"
runat="server" Font-Size="Smaller" Font-Names="Arial Narrow"
ControlToValidate="LstBoxServers" ErrorMessage="The Server Name field is
required."
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="rfvTxtNewServerName" style="Z-INDEX: 125;
LEFT: 675px; POSITION: absolute; TOP: 55px"
runat="server" Font-Size="Smaller" Font-Names="Arial Narrow"
ControlToValidate="TxtNewServerName" ErrorMessage="A new Server Name is
required."
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="rfvTxtNewAdmin" style="Z-INDEX: 126;
LEFT: 472px; POSITION: absolute; TOP: 170px"
runat="server" Font-Size="Smaller" Font-Names="Arial Narrow"
ControlToValidate="TxtNewAdmin" ErrorMessage="A new Admin Name is required."
Display="Dynamic"></asp:RequiredFieldValidator></form>
</body>
</HTML>
 
M

Michael Bray

The code below works correctly with the only exception being that I
cannot capture changes to that TxtDate.Text field. Any thoughts would
be welcome.

If you setting the TxtDate.Text inside Page_Load, then you need to check if
the page is being posted back or if it's the first load, otherwise the
value will be overwritten when you click the Save button. So in Page_Load,
make it look like this:

if (!this.IsPostBack)
{
TxtDate.Text = DateTime.Now.ToShortTimeString();
}

-mdb
 
G

Guest

That worked. Thank you!




Michael Bray said:
If you setting the TxtDate.Text inside Page_Load, then you need to check if
the page is being posted back or if it's the first load, otherwise the
value will be overwritten when you click the Save button. So in Page_Load,
make it look like this:

if (!this.IsPostBack)
{
TxtDate.Text = DateTime.Now.ToShortTimeString();
}

-mdb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top