Default Values being displayed in DetailsView


M

Morris Neuman

sHi,

I have an add button that when clicked displays a blank DetailView form
giving the user the ability to input data before selecting insert or cancel.
The fields on the DetailsView are TextBox and are bound to fields on table 1.


When the blank DetailsView form is displayed, I would like to display from
another table (table 2) default values for the any of the fields if they
exists. The user then does not have to enter this data.

For example in the DetailsView below, I would like to have the scrren show a
value of 10(or from a field from table2) in the NumberOfRetires and "Tranfer
in the FailedRetryAction field. If the form field is a DropDownList then I
can bind it and show the possible values but don't see how if to display a
value from one field and bind to another. I tried DefaultValue but that does
not show on the screen.

<asp:DetailsView ID="DetailsView1" runat="server"
DataSourceID="AccessDataSource1"
Height="50px" AutoGenerateInsertButton="True"
AutoGenerateRows="False"
DefaultMode="Insert" Visible="False" BorderStyle="Solid"
BorderWidth="1px"
CellPadding="5" CellSpacing="1" Font-Names="verdana" Font-Size="8pt"
ForeColor="Navy" Width="125px" BorderColor="#FFC080"
OnItemInserted="DetailsView1_ItemInserted">
<Fields>
<asp:BoundField DataField="AttendantID" HeaderText="Attendant
ID" />

<asp:BoundField DataField="NumberOfRetries"
HeaderText="NumberOfRetries" />
<asp:TemplateField HeaderText="FailedRetryAction">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox11" runat="server" Text='<%#
Bind("FailedRetryAction") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%#
Bind("FailedRetryAction") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameID" HeaderText="NameID" />
<asp:BoundField DataField="AlternateMessageID"
HeaderText="AlternateMessageID" />
</Fields>
 
Ad

Advertisements

A

Allen Chen [MSFT]

Hi Morris:

From your description you wan to assign an initial value to the TextBox11
that displays the data of FailedRetryAction field. Am I right? If so you
can try the following code:
Aspx:
...

<InsertItemTemplate>
<asp:TextBox ID="TextBox11" runat="server"
Text='<%#Bind("FailedRetryAction") %>'
onprerender="TextBox11_PreRender" ></asp:TextBox>

</InsertItemTemplate>

..

Aspx.cs:
protected void TextBox11_PreRender(object sender, EventArgs e)
{
TextBox t = (TextBox)sender;
t.Text = "10";
}

In the above code we got the reference of the TextBox in its PreRender
event handler and assign the initial value í¦10í¦ to it.

Please try it to see if it works and feel free to ask if you have further
questions.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Morris Neuman

Thanks Allen.

Since there are several fields in the DetailsView that I would like to
display data on prerender event I used the onprerender on the DetailsView.

The Prerender on DetailsView works and your example works when I set the
value of textbox1 to a literal.

However I want to set the value of the textbox1 to a value retrieved from a
database that I have defined in AccessDataSource4. I tried the Eval in the
prerender event and get error. In my example below, I want to assign
TextBox1 the value in the field [MaxNew] from the AccessDataSource4 control.
This will show in the Insert DtailsView forn field, the user then can change
or keep as is, then click on Insert to bind to fields in AccessDataSource1.

<asp:DetailsView ID="DetailsView1" runat="server"
DataSourceID="AccessDataSource1"
Height="50px" AutoGenerateInsertButton="True"
AutoGenerateRows="False"
DefaultMode="Insert" BorderStyle="Solid" BorderWidth="1px"
CellPadding="5"
CellSpacing="1" Font-Names="verdana" Font-Size="8pt" ForeColor="Navy"
Width="125px" BorderColor="#FFC080"
OnItemInserted="DetailsView1_ItemInserted"
OnPreRender="DetailsView1_Prerender" style="margin-bottom: 0px">
<Fields>
<asp:BoundField DataField="Key" HeaderText="Key"
InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="BoxNumber" HeaderText="Box Number" >
</asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField HeaderText="First Name"
SortExpression="FirstName" />
<asp:TemplateField HeaderText="MaxNewMessages">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#
Bind("MaxNewMessages") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("MaxNewMessages") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#
Bind("MaxNewMessages") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>


<asp:AccessDataSource ID="AccessDataSource4" runat="server"
DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
SelectCommand="SELECT [MAXNEW], [MAXSAVED], [MAXSECS], [MAXDAYS],
[DEFAULT_FLAGS] FROM [NewAccountDefaults]">
</asp:AccessDataSource>


protected void DetailsView1_Prerender(object sender, EventArgs e)
{
DetailsView dv = (DetailsView)sender;
DetailsView1.DataSourceID = "AccessDataSource4";

TextBox tb1=dv.FindControl("TextBox1") as TextBox;
tb1.Text = DataBinder.Eval(AccessDataSource4, (MaxNew)).ToString();

DetailsView1.DataBind();
}

Hopefully you can tell me what I am doing wrong.
 
A

Allen Chen [MSFT]

Hi Morris,

Thanks for your update. To do this we need to use AccessDataSource4 to
select the record(s) we need. Please firstly add the "Where [ID]=?" Part in
the aspx (Please note, the ID is the name of the primary key. I'm not sure
what that is in your database so I use ID here. Please replace it with the
real primary key):

<asp:AccessDataSource ID="AccessDataSource4" runat="server"
DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
SelectCommand="SELECT [MAXNEW], [MAXSAVED], [MAXSECS], [MAXDAYS],
[DEFAULT_FLAGS] FROM [NewAccountDefaults] Where[ID]=?">
</asp:AccessDataSource>

Then in the aspx.cs we can try this to get the value of MAXNEW field:

AccessDataSource4.SelectParameters.Add("ID","2"); //Please use the correct
primary key name and value instead of ID and 2.
DataView dv=
(DataView)AccessDataSource4.Select(DataSourceSelectArguments.Empty);
//Only one row is returned so we can try
//dv.Table.Rows[0][" MAXNEW"]

If we use following aspx:

<asp:AccessDataSource ID="AccessDataSource4" runat="server"
DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
SelectCommand="SELECT [MAXNEW], [MAXSAVED], [MAXSECS], [MAXDAYS],
[DEFAULT_FLAGS] FROM [NewAccountDefaults]">
</asp:AccessDataSource>

Then in aspx.cs we can try following code:

DataView dv=
(DataView)AccessDataSource4.Select(DataSourceSelectArguments.Empty);
//dv.Table.Rows[index][" MAXNEW"] //index can be 0,1,2, etc. You can test
to see what the MAXNEW is.

to get the value of MAXNEW field.

Please have a try and let me know if you have further questions.

Regards,
Allen Chen
Microsoft Online Support
 
M

Morris Neuman

Hi,

Thanks again for your help. AccessDataSource4 will only have 1 record, but
I appreciate your info on how to read each record (this was going to be one
of my many other questions).

Anyway as I have both AccessDataSource4 and SqlDataSource4 and selection
will depend on the web.config appsetting, I used the code you provided in
another post to find which one.

My PreRender event now is as follows and it works.
protected void DetailsView1_Prerender(object sender, EventArgs e)
{
DetailsView dv = (DetailsView)sender;
TextBox tb1 = dv.FindControl("TextBox1") as TextBox;
TextBox tb2 = dv.FindControl("TextBox2") as TextBox;
TextBox tb3 = dv.FindControl("TextBox3") as TextBox;
TextBox tb4 = dv.FindControl("TextBox4") as TextBox;


string id = ConfigurationManager.AppSettings["MyDataSource"];
Control datasourcecontrol = this.FindControl(id);

//id = "AccessDataSource1";
id = "SqlDataSource1";

if (id.Equals("SqlDataSource1"))
{
DataView dcv =
(DataView)SqlDataSource4.Select(DataSourceSelectArguments.Empty);
int selectrec = 0;
tb1.Text = dcv.Table.Rows[selectrec]["MAXNEW"].ToString();
tb2.Text = dcv.Table.Rows[selectrec]["MAXSAVED"].ToString();
tb3.Text = dcv.Table.Rows[selectrec]["MAXSECS"].ToString();
tb4.Text = dcv.Table.Rows[selectrec]["MAXDAYS"].ToString();
}
else
{
DataView dcv =
(DataView)AccessDataSource4.Select(DataSourceSelectArguments.Empty);
int selectrec = 0;
tb1.Text = dcv.Table.Rows[selectrec]["MAXNEW"].ToString();
tb2.Text = dcv.Table.Rows[selectrec]["MAXSAVED"].ToString();
tb3.Text = dcv.Table.Rows[selectrec]["MAXSECS"].ToString();
tb4.Text = dcv.Table.Rows[selectrec]["MAXDAYS"].ToString();
}
}

One question, when I moved the following tb1.Text = dcv statement outside
the if ... else..., then I get error that The name 'dcv' does not exist in
the current context. The code producing error is as follows:
protected void DetailsView1_Prerender(object sender, EventArgs e)
{
DetailsView dv = (DetailsView)sender;
TextBox tb1 = dv.FindControl("TextBox1") as TextBox;
TextBox tb2 = dv.FindControl("TextBox2") as TextBox;
TextBox tb3 = dv.FindControl("TextBox3") as TextBox;
TextBox tb4 = dv.FindControl("TextBox4") as TextBox;


string id = ConfigurationManager.AppSettings["MyDataSource"];
Control datasourcecontrol = this.FindControl(id);

//id = "AccessDataSource1";
id = "SqlDataSource1";

if (id.Equals("SqlDataSource1"))
{
DataView dcv =
(DataView)SqlDataSource4.Select(DataSourceSelectArguments.Empty);
}
else
{
DataView dcv =
(DataView)AccessDataSource4.Select(DataSourceSelectArguments.Empty);
}

//selectrec can be 0,1,2, etc. depending on the record selected
int selectrec = 0;
tb1.Text = dcv.Table.Rows[selectrec]["MAXNEW"].ToString();
tb2.Text = dcv.Table.Rows[selectrec]["MAXSAVED"].ToString();
tb3.Text = dcv.Table.Rows[selectrec]["MAXSECS"].ToString();
tb4.Text = dcv.Table.Rows[selectrec]["MAXDAYS"].ToString();
}

Once I have set dcv based on the id, why can't I reference the dataset
return values outside the if...else... statement?
--
Once again Allen, I really appreciate your help.

Morris


Allen Chen said:
Hi Morris,

Thanks for your update. To do this we need to use AccessDataSource4 to
select the record(s) we need. Please firstly add the "Where [ID]=?" Part in
the aspx (Please note, the ID is the name of the primary key. I'm not sure
what that is in your database so I use ID here. Please replace it with the
real primary key):

<asp:AccessDataSource ID="AccessDataSource4" runat="server"
DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
SelectCommand="SELECT [MAXNEW], [MAXSAVED], [MAXSECS], [MAXDAYS],
[DEFAULT_FLAGS] FROM [NewAccountDefaults] Where[ID]=?">
</asp:AccessDataSource>

Then in the aspx.cs we can try this to get the value of MAXNEW field:

AccessDataSource4.SelectParameters.Add("ID","2"); //Please use the correct
primary key name and value instead of ID and 2.
DataView dv=
(DataView)AccessDataSource4.Select(DataSourceSelectArguments.Empty);
//Only one row is returned so we can try
//dv.Table.Rows[0][" MAXNEW"]

If we use following aspx:

<asp:AccessDataSource ID="AccessDataSource4" runat="server"
DataFile="C:\Program Files\CallMaster\Data\Callmaster.mdb"
SelectCommand="SELECT [MAXNEW], [MAXSAVED], [MAXSECS], [MAXDAYS],
[DEFAULT_FLAGS] FROM [NewAccountDefaults]">
</asp:AccessDataSource>

Then in aspx.cs we can try following code:

DataView dv=
(DataView)AccessDataSource4.Select(DataSourceSelectArguments.Empty);
//dv.Table.Rows[index][" MAXNEW"] //index can be 0,1,2, etc. You can test
to see what the MAXNEW is.

to get the value of MAXNEW field.

Please have a try and let me know if you have further questions.

Regards,
Allen Chen
Microsoft Online Support
 
A

Allen Chen [MSFT]

Hi Morris,

Thanks for your update.
This is because that the compiler cannot find the definition of the dcv. To
solve this problem we can define the dcv out of the if clause and assign
value to it in the if clause. Like below:

protected void DetailsView1_Prerender(object sender, EventArgs e)
{
DetailsView dv = (DetailsView)sender;
TextBox tb1 = dv.FindControl("TextBox1") as TextBox;
TextBox tb2 = dv.FindControl("TextBox2") as TextBox;
TextBox tb3 = dv.FindControl("TextBox3") as TextBox;
TextBox tb4 = dv.FindControl("TextBox4") as TextBox;


string id = ConfigurationManager.AppSettings["MyDataSource"];
Control datasourcecontrol = this.FindControl(id);

//id = "AccessDataSource1";
id = "SqlDataSource1";
DataView dcv=new DataView();
if (id.Equals("SqlDataSource1"))
{
dcv =
(DataView)SqlDataSource4.Select(DataSourceSelectArguments.Empty);
}
else
{
dcv =
(DataView)AccessDataSource4.Select(DataSourceSelectArguments.Empty);
}

//selectrec can be 0,1,2, etc. depending on the record selected
int selectrec = 0;
tb1.Text = dcv.Table.Rows[selectrec]["MAXNEW"].ToString();
tb2.Text = dcv.Table.Rows[selectrec]["MAXSAVED"].ToString();
tb3.Text = dcv.Table.Rows[selectrec]["MAXSECS"].ToString();
tb4.Text = dcv.Table.Rows[selectrec]["MAXDAYS"].ToString();
}

Please try it to see if it works and feel free to ask if you need further
assistance.

Regards,
Allen Chen
Microsoft Online Support
 
Ad

Advertisements

Ad

Advertisements

A

Allen Chen [MSFT]

Hi Morris,

You're welcome. Thank you for using our Newsgroup Support Service. Have a
nice day!

Regards,
Allen Chen
Microsoft Online Support
 

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