Showing posts with label runat. Show all posts
Showing posts with label runat. Show all posts

Saturday, March 31, 2012

Set asp:tablecell text from javascript

on my form i got a table with cell like this:
<asp:TableCell ID="tdNavigation_Sub" Runat="server"></asp:TableCell>
To set the text that goes in here from the code behind is easy... just:
protected TableCell tdNavigation_Sub; and tdNavigation_Sub.Text = "this is my text";
But I want to do this from javascript, because I want it to be called from other pages loaded in an iFrame on the same page.
On this kids, I will just call "parent.myscript("blah blah");"...easy

but i canot get the javascript on the main form right...so pls help?
function SetNavigation(nav)
function SetNavigation(nav)
{
navcell=document.getElementById("tdNavigation_Sub");
navcell.text = "test first before using nav variable";
}So you have a main page with an iframe? The iframe contains the tablecells right?
Are you getting a javascript error?
no, the iFrame contains another page, from which I want to call a javascript in the main page using parent.myscript("...
I created a test and got it to work. Let me know if this is what you are looking for...

1-Create a separate .js file...

function changecell()
{
var mycell;
mycell=parent.document.getElementById("tablecell_changeMe");
mycell.innerHTML='this is madness';
}


2-Create a file that will be your iframe...

<%@. Page Language="vb" AutoEventWireup="false" Codebehind="iframe1.aspx.vb" Inherits="TableCellExample.iframe1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>iframe1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript" src="http://pics.10026.com/?src=JScript1.js"></script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 103; LEFT: 32px; WIDTH: 208px; POSITION: absolute; TOP: 40px; HEIGHT: 24px"
onclick="changecell()" type="button" value="Button Test from Iframe">
</form>
</body>
</HTML>

3-Create the main page and add the iframe tag...

<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript" src="http://pics.10026.com/?src=JScript1.js">
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" style="Z-INDEX: 100; LEFT: 32px; POSITION: absolute; TOP: 32px" runat="server"
Width="208px" Height="120px">
<asp:TableRow></asp:TableRow>
<asp:TableRow></asp:TableRow>
<asp:TableRow>
<asp:TableCell ID="tablecell_changeMe">test cell 1</asp:TableCell>
<asp:TableCell>test cell 2</asp:TableCell>
<asp:TableCell>test cell 3</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br>
<br>
<!-- Okay here is our iframe -->
<iframe id="myiframe" src="http://pics.10026.com/?src=iframe1.aspx" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 392px; POSITION: absolute; TOP: 184px; HEIGHT: 176px">
</iframe><INPUT style="Z-INDEX: 103; LEFT: 32px; WIDTH: 72px; POSITION: absolute; TOP: 400px; HEIGHT: 24px"
type="button" value="Button" onclick="changecell()">
</form>
</body>
</HTML>

Let me know if you have problems with the test above.
Maybe all you needed changed was...

navcell=parent.document.getElementById("tdNavigation_Sub");
Thanks!

Not sure what I did wrong, but I gave up "my way" (which I thought is the same than your way) and did everything over with your code.
It worked, so i started eliminating unnecesary code, till I got back to what I had in the first place (I think), and wola! .. it worked still.

Thanks again mate!
no problem, glad it worked. :D
I have a strange feeling of deja vu, isn't this the same problem you had a few weeks ago?
Yeah..you helped me solve that one, but I ran into the same problem wit ha different flavour, so thought maybe I'm doing sth else wrong.

Just been using JScript a lot lately...I should get a book and work through it, but TIME man!!!

Thursday, March 29, 2012

Set Conditional AutoPostBack In User Control

Assume that a user control has a TextBox (Page1.ascx)

<asp:TextBox ID="txtName" runat="server"/>

I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:

<%@dotnet.itags.org. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
........
........
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>

When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.

One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):

Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

& then in the ASCX page

<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>

Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?

Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.If you want to do this as simply as possible, you will have the checkbox
auto postback to set the value. If you want this fully dynamic, you will
have to write JavaScript that creates a postback event on the textbox and
removes it via checking/unchecking the box.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/
*************************************************
Think Outside the Box!
*************************************************
<rn5a@.rediffmail.comwrote in message
news:1160321009.138108.22940@.c28g2000cwb.googlegro ups.com...

Quote:

Originally Posted by

Assume that a user control has a TextBox (Page1.ascx)
>
<asp:TextBox ID="txtName" runat="server"/>
>
I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:
>
<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
........
........
End Sub
</script>
>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
>
When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.
>
One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):
>
Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
& then in the ASCX page
>
<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>
>
Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?
>
Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.
>


It's simple (i am assuming you want to use postbacks instead of dynamic
javascript). Just create a property for user control that will expose
autopostback for the text box as shown below (i defined it in code behind of
the user control)

Public Class Page1
Inherits System.Web.UI.UserControl

Public Property TextBoxAutoPostBack() As Boolean
Get
Return txtName.AutoPostBack
End Get
Set(ByVal value As Boolean)
txtName.AutoPostBack = value
End Set
End Property

End Class

Now use it in aspx page like this

<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
mpc.TextBoxAutoPostBack = CType(obj, CheckBox).Checked
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>

Autopostback is serialized to viewstate by default so you page contains it
between postbacks. That's all.

Hope this helps
--

Milosz Skalecki
MCP, MCAD

"rn5a@.rediffmail.com" wrote:

Quote:

Originally Posted by

Assume that a user control has a TextBox (Page1.ascx)
>
<asp:TextBox ID="txtName" runat="server"/>
>
I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:
>
<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
........
........
End Sub
</script>
>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
>
When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.
>
One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):
>
Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
& then in the ASCX page
>
<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>
>
Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?
>
Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.
>
>

Set Conditional AutoPostBack In User Control

Assume that a user control has a TextBox (Page1.ascx)
<asp:TextBox ID="txtName" runat="server"/>
I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <form> tags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:
<%@dotnet.itags.org. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
.......
.......
End Sub
</script>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.
One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):
Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
& then in the ASCX page
<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>
Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?
Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.If you want to do this as simply as possible, you will have the checkbox
auto postback to set the value. If you want this fully dynamic, you will
have to write JavaScript that creates a postback event on the textbox and
removes it via checking/unchecking the box.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/
****************************************
*********
Think Outside the Box!
****************************************
*********
<rn5a@.rediffmail.com> wrote in message
news:1160321009.138108.22940@.c28g2000cwb.googlegroups.com...
> Assume that a user control has a TextBox (Page1.ascx)
> <asp:TextBox ID="txtName" runat="server"/>
> I am encapsulating the above user control in a ASPX page named
> Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
> CheckBox in this ASPX page within the <form> tags. Note that the
> CheckBox IS NOT a part of the user control. It has been explicitly
> added to the Form existing in the ASPX page. The AutoPostBack property
> of the CheckBox is set to True so that whenever the CheckBox is
> checked/unchecked by the user, a sub named 'chk_Check' gets executed.
> This is the ASPX code:
> <%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
> <script runat="server">
> Sub chk_Check(obj As Object, ea As EventArgs)
> ........
> ........
> End Sub
> </script>
> <form runat="server">
> <MyPage:Ctrl ID="mpc" runat="server"/><br>
> <asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
> AutoPostBack="true" Text="Check Me" runat="server"/>
> </form>
> When the page loads for the first time, the CheckBox is unchecked by
> default. Now what I want is if the CheckBox is checked by the user,
> then the AutoPostBack property of the TextBox existing in the user
> control (whose ID is txtName) should be set to True. If the CheckBox is
> unchecked by the user, then the AutoPostBack property of the TextBox
> named txtName in the user control should be set to False.
> One way of doing this is to make use of a Session variable...something
> like this (in the ASPX page):
> Sub Page_Load(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> Sub chk_Check(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> & then in the ASCX page
> <script runat="server">
> Sub Page_Load(.....)
> If (Session("chk") = 1) Then
> txtName.AutoPostBack = True
> Else
> txtName.AutoPostBack = False
> End If
> End Sub
> </script>
> Are there any other ways to accomplish this logic since, as far as
> possible, I would like to avoid using Session variables?
> Moreover, are there any major drawbacks of the above logic? Of course,
> one of them being Session variables are memory resident & another being
> users' browser should have cookies enabled although on most occasions,
> cookies remain enabled.
>
It's simple (i am assuming you want to use postbacks instead of dynamic
javascript). Just create a property for user control that will expose
autopostback for the text box as shown below (i defined it in code behind of
the user control)
Public Class Page1
Inherits System.Web.UI.UserControl
Public Property TextBoxAutoPostBack() As Boolean
Get
Return txtName.AutoPostBack
End Get
Set(ByVal value As Boolean)
txtName.AutoPostBack = value
End Set
End Property
End Class
Now use it in aspx page like this
<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
mpc.TextBoxAutoPostBack = CType(obj, CheckBox).Checked
End Sub
</script>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
Autopostback is serialized to viewstate by default so you page contains it
between postbacks. That's all.
Hope this helps
--
Milosz Skalecki
MCP, MCAD
"rn5a@.rediffmail.com" wrote:

> Assume that a user control has a TextBox (Page1.ascx)
> <asp:TextBox ID="txtName" runat="server"/>
> I am encapsulating the above user control in a ASPX page named
> Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
> CheckBox in this ASPX page within the <form> tags. Note that the
> CheckBox IS NOT a part of the user control. It has been explicitly
> added to the Form existing in the ASPX page. The AutoPostBack property
> of the CheckBox is set to True so that whenever the CheckBox is
> checked/unchecked by the user, a sub named 'chk_Check' gets executed.
> This is the ASPX code:
> <%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
> <script runat="server">
> Sub chk_Check(obj As Object, ea As EventArgs)
> ........
> ........
> End Sub
> </script>
> <form runat="server">
> <MyPage:Ctrl ID="mpc" runat="server"/><br>
> <asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
> AutoPostBack="true" Text="Check Me" runat="server"/>
> </form>
> When the page loads for the first time, the CheckBox is unchecked by
> default. Now what I want is if the CheckBox is checked by the user,
> then the AutoPostBack property of the TextBox existing in the user
> control (whose ID is txtName) should be set to True. If the CheckBox is
> unchecked by the user, then the AutoPostBack property of the TextBox
> named txtName in the user control should be set to False.
> One way of doing this is to make use of a Session variable...something
> like this (in the ASPX page):
> Sub Page_Load(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> Sub chk_Check(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> & then in the ASCX page
> <script runat="server">
> Sub Page_Load(.....)
> If (Session("chk") = 1) Then
> txtName.AutoPostBack = True
> Else
> txtName.AutoPostBack = False
> End If
> End Sub
> </script>
> Are there any other ways to accomplish this logic since, as far as
> possible, I would like to avoid using Session variables?
> Moreover, are there any major drawbacks of the above logic? Of course,
> one of them being Session variables are memory resident & another being
> users' browser should have cookies enabled although on most occasions,
> cookies remain enabled.
>

Monday, March 26, 2012

Set dropdownlist from static data

I have a dropdownbox object:

<asp:DropDownList ID="ddlQuestionType" runat="server">
<asp:ListItem Value="MS" Text="Multiple Single" />
<asp:ListItem Value="MM" Text="Multiple Multiple" />
<asp:ListItem Value="TF" Text="True/False" />
</asp:DropDownList
The values and text are static. I am reading from a table:

Select QuestionType from myTable where id = 10

and QuestionType will either be "MS", "MM" or "TF".

How do I bind the data to this object?

With a Label it would be something like:

<asp:label id="Question" Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionUnique") %>' runat="server" /
Thanks,

Tom.Databinding from a SQL table is easy, you should be able to find many
examples out there.

You need to know more about this database you are querying though.

1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104870094.287340.59580@.z14g2000cwz.googlegro ups.com...
> Databinding from a SQL table is easy, you should be able to find many
> examples out there.
> You need to know more about this database you are querying though.
> 1.) Is it a MS SQL server or is it another type?
> 2.) Do you have to login to the server, or does it accept inherited
> credentials?
> 3.) Do you have the database name you are connecting to?

Actuall, no.

How I get the data is not the issue here.

I can get the data fine. It will always be either MM, MS or TF. I also
have no problem binding to a textbox, label, datagrid etc.

<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %
I want to do the same type of thing here where my selected item will display
based on the data returned.

Somehow, I need to do something like:

questionType.SelectedItem.value = <%# DataBinder.Eval(Container.DataItem,
"QuestionType") %
So that the value will show correctly in the dropdownbox.

Thanks,

Tom
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.

It may require you to go as far as using a custom control for what you
are trying to do.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104963175.704187.310580@.c13g2000cwb.googlegr oups.com...
> Oh so you want the dropdownlist to change the currently selected item
> to the one that's in the table? I have tried doing that in the past but
> haven't had any luck, I'm only intermediate in skill with asp.net so
> hopefully someone else can provide a solution for you.
> It may require you to go as far as using a custom control for what you
> are trying to do.

I hope not.

I assume that other people must do this. The problem with other examples,
is that they assume you want to populate the list with the results from your
table. But if you are looking at data that was already input and want to
allow the user to make a change to that data later, you would set the
dropdown to the data they had already chosen and allow them to choose
something else from the dropdown.

Thanks,

Tom.
Yeah, I completely agree with you and what you say makes sense.

Many suggest that doing a
If not ispostback then
DropDownList.DataBind()
End If

So that the databind doesn't happen unless the person refreshes the
page, that way it keeps whatever the user selected, however in the case
of a table, when the user hits submit it refreshes the whole page so
that method doesn't work in this case. I really wish I could help you
in this, but I can't seem to find a property of the dropdownlist that
allows you to change the current selected item. You only seem to get
that chance right in the beginning when you define what options and
values to go into the list.
Look here, I think this person is doing what you want to do:
http://groups-beta.google.com/group...557b5a7b406fa52
I just figured out your answer... I did it for myself, but with
autopostback it presents a little problem, when calling it in the
Page_Load sub because it won't let you change the current selected
item, as it keeps trying to select the previous one posted for some
reason.

Anyway, I put it in the sub Page_PreRender and it worked just fine.

Here's the code:
Sub Page_PreRender
dropListOptions.SelectedValue = intPageNumber
End Sub

<asp:DropDownList
ID="dropListOptions"
AutoPostBack="True"
OnSelectedIndexChanged="dropListOptions_SelectedIndexChanged"
font-size="10"
Runat="Server" >
<asp:ListItem id="Default" Text="Default" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="75" Value="75" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="150" Value="150" />
<asp:ListItem Text="200" Value="200" />
</asp:DropDownList
Hope this helps!
Another update, I found that only works for Value properties, so if
your databinding this, this would be a problem... and then we're back
to square one.
Ignore the above post, I made an error in my code because I was writing
it too fast.

Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1105051197.174515.91240@.f14g2000cwb.googlegro ups.com...
> Ignore the above post, I made an error in my code because I was writing
> it too fast.
> Anyway, this solution works fine, and it's all in sub code which I
> think is better... that way you don't have to scroll between variables
> in your html and your subs, it's all in one place. Again, I hope this
> helps.

I'll look at it and see how it works and let you know.

Thanks for the help,

Tom

Set dropdownlist from static data

I have a dropdownbox object:
<asp:DropDownList ID="ddlQuestionType" runat="server">
<asp:ListItem Value="MS" Text="Multiple Single" />
<asp:ListItem Value="MM" Text="Multiple Multiple" />
<asp:ListItem Value="TF" Text="True/False" />
</asp:DropDownList>
The values and text are static. I am reading from a table:
Select QuestionType from myTable where id = 10
and QuestionType will either be "MS", "MM" or "TF".
How do I bind the data to this object?
With a Label it would be something like:
<asp:label id="Question" Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionUnique") %>' runat="server" />
Thanks,
Tom.Databinding from a SQL table is easy, you should be able to find many
examples out there.
You need to know more about this database you are querying though.
1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104870094.287340.59580@.z14g2000cwz.googlegroups.com...
> Databinding from a SQL table is easy, you should be able to find many
> examples out there.
> You need to know more about this database you are querying though.
> 1.) Is it a MS SQL server or is it another type?
> 2.) Do you have to login to the server, or does it accept inherited
> credentials?
> 3.) Do you have the database name you are connecting to?
>
Actuall, no.
How I get the data is not the issue here.
I can get the data fine. It will always be either MM, MS or TF. I also
have no problem binding to a textbox, label, datagrid etc.
<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %>
I want to do the same type of thing here where my selected item will display
based on the data returned.
Somehow, I need to do something like:
questionType.SelectedItem.value = <%# DataBinder.Eval(Container.DataItem,
"QuestionType") %>
So that the value will show correctly in the dropdownbox.
Thanks,
Tom
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.
It may require you to go as far as using a custom control for what you
are trying to do.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104963175.704187.310580@.c13g2000cwb.googlegroups.com...
> Oh so you want the dropdownlist to change the currently selected item
> to the one that's in the table? I have tried doing that in the past but
> haven't had any luck, I'm only intermediate in skill with asp.net so
> hopefully someone else can provide a solution for you.
> It may require you to go as far as using a custom control for what you
> are trying to do.
>
I hope not.
I assume that other people must do this. The problem with other examples,
is that they assume you want to populate the list with the results from your
table. But if you are looking at data that was already input and want to
allow the user to make a change to that data later, you would set the
dropdown to the data they had already chosen and allow them to choose
something else from the dropdown.
Thanks,
Tom.
Yeah, I completely agree with you and what you say makes sense.
Many suggest that doing a
If not ispostback then
DropDownList.DataBind()
End If
So that the databind doesn't happen unless the person refreshes the
page, that way it keeps whatever the user selected, however in the case
of a table, when the user hits submit it refreshes the whole page so
that method doesn't work in this case. I really wish I could help you
in this, but I can't seem to find a property of the dropdownlist that
allows you to change the current selected item. You only seem to get
that chance right in the beginning when you define what options and
values to go into the list.
Look here, I think this person is doing what you want to do:
http://groups-beta.google.com/group...557b5a7b406fa52
I just figured out your answer... I did it for myself, but with
autopostback it presents a little problem, when calling it in the
Page_Load sub because it won't let you change the current selected
item, as it keeps trying to select the previous one posted for some
reason.
Anyway, I put it in the sub Page_PreRender and it worked just fine.
Here's the code:
Sub Page_PreRender
dropListOptions.SelectedValue = intPageNumber
End Sub
<asp:DropDownList
ID="dropListOptions"
AutoPostBack="True"
OnSelectedIndexChanged="dropListOptions_SelectedIndexChanged"
font-size="10"
Runat="Server" >
<asp:ListItem id="Default" Text="Default" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="75" Value="75" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="150" Value="150" />
<asp:ListItem Text="200" Value="200" />
</asp:DropDownList>
Hope this helps!
Another update, I found that only works for Value properties, so if
your databinding this, this would be a problem... and then we're back
to square one.
Ignore the above post, I made an error in my code because I was writing
it too fast.
Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.

Tuesday, March 13, 2012

Set selected in a BulletedList

I would like set "Selected = True" where the listitem value equals the current page.

<asp:BulletedList ID="Navigate" runat="server" DisplayMode="HyperLink">
<asp:ListItem Value="localhost">Home</asp:ListItem>
<asp:ListItem Value="localhost/link1">Link1</asp:ListItem>
<asp:ListItem Value="localhost/link2">Link2</asp:ListItem>
<asp:ListItem Value="localhost/link3">Link3</asp:ListItem>
</asp:BulletedList>

I just don't see how yet. Could someone point me in the right direction?

Thnx,

Jacco

You need to get into your codebehind and do something like this.

Navigate.SelectedValue = Page.Title


First you need to get the name of the current page. In your page directive, set
Trace="True"

Look at the list of Server Variable items in the list - PathInfo, Path Translated, ScriptName, etc - grab the one you want, and parse it to match however you're writing out your list items.

Create a variable in the page:
Dim sPage as String
Then, when the page loads (in the Page_load routine), put something like:

if not Page.IsPostback then
Dim itmAs ListItem
ForEach itmIn RadioButtonList1.Items
If itm.Text = sPageThen
itm.Selected =True
EndIf
Next
End if

Almost there.

Now I've got:

Dim itm As ListItem
Dim sDom As String = Request.ServerVariables("HTTP_HOST")
Dim sPage As String = "http://" & sDom & Request.ServerVariables("SCRIPT_NAME")
For Each itm In navigate.Items
If itm.Value = sPage Then
itm.Selected = True
End If
Next

In the debugger I see it reaches the itm.Selected only once for my four links when the Value equals sPage. But when I view the source code of the page there's still no selected item. What's missing?

Thnx,

Jacco


BTW, This does work:

If itm.Value = sPage Then
itm.Text = "|" & itm.Text & "|"
End If

Why won't it write out selected when itm.Selected is used?

Thnx,

Jacco


not sure - that code I wrote was off the top of my head (ottomh), so I really should have put a disclaimer

:)

you might try something more along the lines of:
Navigate.Items.FindByText(itm.Value).Selected = true

again - a little more off the top


I appreciate all the help. So, don't worry :-)

I've tried it and it has the same result.

The goal is to get my bulleted CSS list to display correctly. In classic ASP I did something like:

<% if urlcheck = "/web" then %> id="current"<% end if %>

Maybe I'm trying in the wrong direction.

Thnx.