Showing posts with label postback. Show all posts
Showing posts with label postback. Show all posts

Wednesday, March 28, 2012

TableRows and Postback

I've been at this all day and it's really starting to annoy me now. I have a
form, this has a textbox which the user enters a search parameter into. This
is passed to a Stored Procedure as a parameter, the results returned are the
n
bound to a GridView control. This GridView control has an extra column with
a
checkbox in it for every row. The idea for this being that the user selects
which items he/she would like to use by checking the checkboxes.
Once they have made their selection, they then click a button and this
copies their selections over to an ASP:Table control. The table control is
declaratively created with each table row being dynamically created.
All of the above works fine using the code included in this post. The
problems begin if they then start another search, they want to keep the
values in the table but the table is cleared and populated with the new
values when they add the results of their new search to it.
STEP: User performs a search.
RESULT: GridView populated with results of search
STEP: User selects items they want to use from the GridView and then click
on the Add To List button.
RESULT: Table control has x amount of rows added containing the details as
they appear in the GridView.
STEP: User then selects more items from the same search.
RESULT: The items selected are appended to the table.
STEP: User performs another search and selects the items from those search
results.
RESULT: Table only shows the items that the user has just selected, all the
previous items are lost
I've taken a screenshot of the test form to help you visualize it.
(http://mparter.pwp.blueyonder.co.uk/dyntable.png)
Please save me before I go mad
Thanks.
---CODE--
--
Protected Sub AddToList()
Dim gridRow As GridViewRow
For Each gridRow In GridView1.Rows
Dim gridCell0 As TableCell = gridRow.Cells(0)
Dim gridCell1 As TableCell = gridRow.Cells(1)
Dim gridCell2 As TableCell = gridRow.Cells(2)
Dim gridCell3 As TableCell = gridRow.Cells(3)
Dim gridCell4 As TableCell = gridRow.Cells(4)
Dim chkStudent As HtmlInputCheckBox =
CType(gridCell0.Controls(1), HtmlInputCheckBox)
If Not (chkStudent Is Nothing) And chkStudent.Checked Then
Dim tblRow As New TableRow
Dim tblCell0 As New TableCell
Dim tblCell1 As New TableCell
Dim tblCell2 As New TableCell
Dim tblCell3 As New TableCell
Dim tblCell4 As New TableCell
Dim chkSelectedStudent As New HtmlInputCheckBox
tblCell0.Controls.Add(chkSelectedStudent)
chkSelectedStudent.ID = "chkSelectedStudent_" & gridCell1.Text
chkSelectedStudent.Value = gridCell1.Text
tblRow.Cells.Add(tblCell0)
tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
tblRow.Cells.Add(tblCell1)
tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
tblRow.Cells.Add(tblCell2)
tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
tblRow.Cells.Add(tblCell3)
tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
tblRow.Cells.Add(tblCell4)
Table1.Rows.Add(tblRow)
End If
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Label1.Text = DateTime.Now.ToString
If Page.IsPostBack Then
AddToList()
End If
End SubI think you need to create a session variable to contain the selected rows
(i.e. Session("AddedItemsTable")=AddedItemsTable, that way previous
selections will be remembered between postbacks.
"Mark Parter" <MarkParter@.discussions.microsoft.com> wrote in message
news:6F3CD5F3-450F-4663-AE2E-91A5F329EFC7@.microsoft.com...
> I've been at this all day and it's really starting to annoy me now. I have
> a
> form, this has a textbox which the user enters a search parameter into.
> This
> is passed to a Stored Procedure as a parameter, the results returned are
> then
> bound to a GridView control. This GridView control has an extra column
> with a
> checkbox in it for every row. The idea for this being that the user
> selects
> which items he/she would like to use by checking the checkboxes.
> Once they have made their selection, they then click a button and this
> copies their selections over to an ASP:Table control. The table control is
> declaratively created with each table row being dynamically created.
> All of the above works fine using the code included in this post. The
> problems begin if they then start another search, they want to keep the
> values in the table but the table is cleared and populated with the new
> values when they add the results of their new search to it.
> STEP: User performs a search.
> RESULT: GridView populated with results of search
> STEP: User selects items they want to use from the GridView and then click
> on the Add To List button.
> RESULT: Table control has x amount of rows added containing the details as
> they appear in the GridView.
> STEP: User then selects more items from the same search.
> RESULT: The items selected are appended to the table.
> STEP: User performs another search and selects the items from those search
> results.
> RESULT: Table only shows the items that the user has just selected, all
> the
> previous items are lost
>
> I've taken a screenshot of the test form to help you visualize it.
> (http://mparter.pwp.blueyonder.co.uk/dyntable.png)
> Please save me before I go mad
> Thanks.
> ---CODE--
--
> Protected Sub AddToList()
> Dim gridRow As GridViewRow
> For Each gridRow In GridView1.Rows
> Dim gridCell0 As TableCell = gridRow.Cells(0)
> Dim gridCell1 As TableCell = gridRow.Cells(1)
> Dim gridCell2 As TableCell = gridRow.Cells(2)
> Dim gridCell3 As TableCell = gridRow.Cells(3)
> Dim gridCell4 As TableCell = gridRow.Cells(4)
> Dim chkStudent As HtmlInputCheckBox =
> CType(gridCell0.Controls(1), HtmlInputCheckBox)
> If Not (chkStudent Is Nothing) And chkStudent.Checked Then
> Dim tblRow As New TableRow
> Dim tblCell0 As New TableCell
> Dim tblCell1 As New TableCell
> Dim tblCell2 As New TableCell
> Dim tblCell3 As New TableCell
> Dim tblCell4 As New TableCell
> Dim chkSelectedStudent As New HtmlInputCheckBox
> tblCell0.Controls.Add(chkSelectedStudent)
> chkSelectedStudent.ID = "chkSelectedStudent_" &
> gridCell1.Text
> chkSelectedStudent.Value = gridCell1.Text
> tblRow.Cells.Add(tblCell0)
> tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
> tblRow.Cells.Add(tblCell1)
> tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
> tblRow.Cells.Add(tblCell2)
> tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
> tblRow.Cells.Add(tblCell3)
> tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
> tblRow.Cells.Add(tblCell4)
> Table1.Rows.Add(tblRow)
> End If
> Next
> End Sub
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Label1.Text = DateTime.Now.ToString
> If Page.IsPostBack Then
> AddToList()
> End If
> End Sub
The changes you are making on the client on not known by the server. Since
<tr> and <td> tags are not postable, their new values never make it back to
the server for processing.
One workaround is to use a <input type="hidden"> element on the page as this
is postable. When you are client side adding rows to your table, you should
alter the value of your hidden field. On postback, you parse the hidden
field and on the server, create the rows and columns you need. You will
need two delimiters, one for each column, and one for a new row:
"col1.1^col1.2^col1.3~col1.2^col2.2^col2.3"
<asp:Table id="tblAddons" runat="server" />
Page_Load
{
string tableValue = String.Empty;
if ( Page.IsPostBack )
{
tableValue = Request.Forms["tblAddons_Value"];
RebuildHtmlTable( tableValue );
}
//always register this hidden field with the previous value.
Page.RegisterHiddenField( "tblAddons_Text", tableValue );
}
//in this method you will create the TableRow and TableCell items based on
the string passed to it.
RebuildHtmlTable( string instructions )
HTH,
bill
"Mark Parter" <MarkParter@.discussions.microsoft.com> wrote in message
news:6F3CD5F3-450F-4663-AE2E-91A5F329EFC7@.microsoft.com...
> I've been at this all day and it's really starting to annoy me now. I have
a
> form, this has a textbox which the user enters a search parameter into.
This
> is passed to a Stored Procedure as a parameter, the results returned are
then
> bound to a GridView control. This GridView control has an extra column
with a
> checkbox in it for every row. The idea for this being that the user
selects
> which items he/she would like to use by checking the checkboxes.
> Once they have made their selection, they then click a button and this
> copies their selections over to an ASP:Table control. The table control is
> declaratively created with each table row being dynamically created.
> All of the above works fine using the code included in this post. The
> problems begin if they then start another search, they want to keep the
> values in the table but the table is cleared and populated with the new
> values when they add the results of their new search to it.
> STEP: User performs a search.
> RESULT: GridView populated with results of search
> STEP: User selects items they want to use from the GridView and then click
> on the Add To List button.
> RESULT: Table control has x amount of rows added containing the details as
> they appear in the GridView.
> STEP: User then selects more items from the same search.
> RESULT: The items selected are appended to the table.
> STEP: User performs another search and selects the items from those search
> results.
> RESULT: Table only shows the items that the user has just selected, all
the
> previous items are lost
>
> I've taken a screenshot of the test form to help you visualize it.
> (http://mparter.pwp.blueyonder.co.uk/dyntable.png)
> Please save me before I go mad
> Thanks.
> ---CODE--
--
> Protected Sub AddToList()
> Dim gridRow As GridViewRow
> For Each gridRow In GridView1.Rows
> Dim gridCell0 As TableCell = gridRow.Cells(0)
> Dim gridCell1 As TableCell = gridRow.Cells(1)
> Dim gridCell2 As TableCell = gridRow.Cells(2)
> Dim gridCell3 As TableCell = gridRow.Cells(3)
> Dim gridCell4 As TableCell = gridRow.Cells(4)
> Dim chkStudent As HtmlInputCheckBox =
> CType(gridCell0.Controls(1), HtmlInputCheckBox)
> If Not (chkStudent Is Nothing) And chkStudent.Checked Then
> Dim tblRow As New TableRow
> Dim tblCell0 As New TableCell
> Dim tblCell1 As New TableCell
> Dim tblCell2 As New TableCell
> Dim tblCell3 As New TableCell
> Dim tblCell4 As New TableCell
> Dim chkSelectedStudent As New HtmlInputCheckBox
> tblCell0.Controls.Add(chkSelectedStudent)
> chkSelectedStudent.ID = "chkSelectedStudent_" &
gridCell1.Text
> chkSelectedStudent.Value = gridCell1.Text
> tblRow.Cells.Add(tblCell0)
> tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
> tblRow.Cells.Add(tblCell1)
> tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
> tblRow.Cells.Add(tblCell2)
> tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
> tblRow.Cells.Add(tblCell3)
> tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
> tblRow.Cells.Add(tblCell4)
> Table1.Rows.Add(tblRow)
> End If
> Next
> End Sub
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Label1.Text = DateTime.Now.ToString
> If Page.IsPostBack Then
> AddToList()
> End If
> End Sub

TableRows and Postback

I've been at this all day and it's really starting to annoy me now. I have a
form, this has a textbox which the user enters a search parameter into. This
is passed to a Stored Procedure as a parameter, the results returned are then
bound to a GridView control. This GridView control has an extra column with a
checkbox in it for every row. The idea for this being that the user selects
which items he/she would like to use by checking the checkboxes.

Once they have made their selection, they then click a button and this
copies their selections over to an ASP:Table control. The table control is
declaratively created with each table row being dynamically created.

All of the above works fine using the code included in this post. The
problems begin if they then start another search, they want to keep the
values in the table but the table is cleared and populated with the new
values when they add the results of their new search to it.

STEP: User performs a search.
RESULT: GridView populated with results of search

STEP: User selects items they want to use from the GridView and then click
on the Add To List button.
RESULT: Table control has x amount of rows added containing the details as
they appear in the GridView.

STEP: User then selects more items from the same search.
RESULT: The items selected are appended to the table.

STEP: User performs another search and selects the items from those search
results.
RESULT: Table only shows the items that the user has just selected, all the
previous items are lost

I've taken a screenshot of the test form to help you visualize it.
(http://mparter.pwp.blueyonder.co.uk/dyntable.png)

Please save me before I go mad

Thanks.

-------------CODE---------------

Protected Sub AddToList()
Dim gridRow As GridViewRow

For Each gridRow In GridView1.Rows

Dim gridCell0 As TableCell = gridRow.Cells(0)
Dim gridCell1 As TableCell = gridRow.Cells(1)
Dim gridCell2 As TableCell = gridRow.Cells(2)
Dim gridCell3 As TableCell = gridRow.Cells(3)
Dim gridCell4 As TableCell = gridRow.Cells(4)

Dim chkStudent As HtmlInputCheckBox =
CType(gridCell0.Controls(1), HtmlInputCheckBox)
If Not (chkStudent Is Nothing) And chkStudent.Checked Then
Dim tblRow As New TableRow
Dim tblCell0 As New TableCell
Dim tblCell1 As New TableCell
Dim tblCell2 As New TableCell
Dim tblCell3 As New TableCell
Dim tblCell4 As New TableCell

Dim chkSelectedStudent As New HtmlInputCheckBox

tblCell0.Controls.Add(chkSelectedStudent)
chkSelectedStudent.ID = "chkSelectedStudent_" & gridCell1.Text
chkSelectedStudent.Value = gridCell1.Text
tblRow.Cells.Add(tblCell0)
tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
tblRow.Cells.Add(tblCell1)
tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
tblRow.Cells.Add(tblCell2)
tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
tblRow.Cells.Add(tblCell3)
tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
tblRow.Cells.Add(tblCell4)

Table1.Rows.Add(tblRow)
End If
Next
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Label1.Text = DateTime.Now.ToString
If Page.IsPostBack Then
AddToList()
End If
End SubI think you need to create a session variable to contain the selected rows
(i.e. Session("AddedItemsTable")=AddedItemsTable, that way previous
selections will be remembered between postbacks.

"Mark Parter" <MarkParter@.discussions.microsoft.com> wrote in message
news:6F3CD5F3-450F-4663-AE2E-91A5F329EFC7@.microsoft.com...
> I've been at this all day and it's really starting to annoy me now. I have
> a
> form, this has a textbox which the user enters a search parameter into.
> This
> is passed to a Stored Procedure as a parameter, the results returned are
> then
> bound to a GridView control. This GridView control has an extra column
> with a
> checkbox in it for every row. The idea for this being that the user
> selects
> which items he/she would like to use by checking the checkboxes.
> Once they have made their selection, they then click a button and this
> copies their selections over to an ASP:Table control. The table control is
> declaratively created with each table row being dynamically created.
> All of the above works fine using the code included in this post. The
> problems begin if they then start another search, they want to keep the
> values in the table but the table is cleared and populated with the new
> values when they add the results of their new search to it.
> STEP: User performs a search.
> RESULT: GridView populated with results of search
> STEP: User selects items they want to use from the GridView and then click
> on the Add To List button.
> RESULT: Table control has x amount of rows added containing the details as
> they appear in the GridView.
> STEP: User then selects more items from the same search.
> RESULT: The items selected are appended to the table.
> STEP: User performs another search and selects the items from those search
> results.
> RESULT: Table only shows the items that the user has just selected, all
> the
> previous items are lost
>
> I've taken a screenshot of the test form to help you visualize it.
> (http://mparter.pwp.blueyonder.co.uk/dyntable.png)
> Please save me before I go mad
> Thanks.
> -------------CODE---------------
> Protected Sub AddToList()
> Dim gridRow As GridViewRow
> For Each gridRow In GridView1.Rows
> Dim gridCell0 As TableCell = gridRow.Cells(0)
> Dim gridCell1 As TableCell = gridRow.Cells(1)
> Dim gridCell2 As TableCell = gridRow.Cells(2)
> Dim gridCell3 As TableCell = gridRow.Cells(3)
> Dim gridCell4 As TableCell = gridRow.Cells(4)
> Dim chkStudent As HtmlInputCheckBox =
> CType(gridCell0.Controls(1), HtmlInputCheckBox)
> If Not (chkStudent Is Nothing) And chkStudent.Checked Then
> Dim tblRow As New TableRow
> Dim tblCell0 As New TableCell
> Dim tblCell1 As New TableCell
> Dim tblCell2 As New TableCell
> Dim tblCell3 As New TableCell
> Dim tblCell4 As New TableCell
> Dim chkSelectedStudent As New HtmlInputCheckBox
> tblCell0.Controls.Add(chkSelectedStudent)
> chkSelectedStudent.ID = "chkSelectedStudent_" &
> gridCell1.Text
> chkSelectedStudent.Value = gridCell1.Text
> tblRow.Cells.Add(tblCell0)
> tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
> tblRow.Cells.Add(tblCell1)
> tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
> tblRow.Cells.Add(tblCell2)
> tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
> tblRow.Cells.Add(tblCell3)
> tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
> tblRow.Cells.Add(tblCell4)
> Table1.Rows.Add(tblRow)
> End If
> Next
> End Sub
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Label1.Text = DateTime.Now.ToString
> If Page.IsPostBack Then
> AddToList()
> End If
> End Sub
The changes you are making on the client on not known by the server. Since
<tr> and <td> tags are not postable, their new values never make it back to
the server for processing.

One workaround is to use a <input type="hidden"> element on the page as this
is postable. When you are client side adding rows to your table, you should
alter the value of your hidden field. On postback, you parse the hidden
field and on the server, create the rows and columns you need. You will
need two delimiters, one for each column, and one for a new row:

"col1.1^col1.2^col1.3~col1.2^col2.2^col2.3"

<asp:Table id="tblAddons" runat="server" /
Page_Load
{
string tableValue = String.Empty;
if ( Page.IsPostBack )
{
tableValue = Request.Forms["tblAddons_Value"];
RebuildHtmlTable( tableValue );
}

//always register this hidden field with the previous value.
Page.RegisterHiddenField( "tblAddons_Text", tableValue );
}

//in this method you will create the TableRow and TableCell items based on
the string passed to it.
RebuildHtmlTable( string instructions )

HTH,

bill
"Mark Parter" <MarkParter@.discussions.microsoft.com> wrote in message
news:6F3CD5F3-450F-4663-AE2E-91A5F329EFC7@.microsoft.com...
> I've been at this all day and it's really starting to annoy me now. I have
a
> form, this has a textbox which the user enters a search parameter into.
This
> is passed to a Stored Procedure as a parameter, the results returned are
then
> bound to a GridView control. This GridView control has an extra column
with a
> checkbox in it for every row. The idea for this being that the user
selects
> which items he/she would like to use by checking the checkboxes.
> Once they have made their selection, they then click a button and this
> copies their selections over to an ASP:Table control. The table control is
> declaratively created with each table row being dynamically created.
> All of the above works fine using the code included in this post. The
> problems begin if they then start another search, they want to keep the
> values in the table but the table is cleared and populated with the new
> values when they add the results of their new search to it.
> STEP: User performs a search.
> RESULT: GridView populated with results of search
> STEP: User selects items they want to use from the GridView and then click
> on the Add To List button.
> RESULT: Table control has x amount of rows added containing the details as
> they appear in the GridView.
> STEP: User then selects more items from the same search.
> RESULT: The items selected are appended to the table.
> STEP: User performs another search and selects the items from those search
> results.
> RESULT: Table only shows the items that the user has just selected, all
the
> previous items are lost
>
> I've taken a screenshot of the test form to help you visualize it.
> (http://mparter.pwp.blueyonder.co.uk/dyntable.png)
> Please save me before I go mad
> Thanks.
> -------------CODE---------
-------
> Protected Sub AddToList()
> Dim gridRow As GridViewRow
> For Each gridRow In GridView1.Rows
> Dim gridCell0 As TableCell = gridRow.Cells(0)
> Dim gridCell1 As TableCell = gridRow.Cells(1)
> Dim gridCell2 As TableCell = gridRow.Cells(2)
> Dim gridCell3 As TableCell = gridRow.Cells(3)
> Dim gridCell4 As TableCell = gridRow.Cells(4)
> Dim chkStudent As HtmlInputCheckBox =
> CType(gridCell0.Controls(1), HtmlInputCheckBox)
> If Not (chkStudent Is Nothing) And chkStudent.Checked Then
> Dim tblRow As New TableRow
> Dim tblCell0 As New TableCell
> Dim tblCell1 As New TableCell
> Dim tblCell2 As New TableCell
> Dim tblCell3 As New TableCell
> Dim tblCell4 As New TableCell
> Dim chkSelectedStudent As New HtmlInputCheckBox
> tblCell0.Controls.Add(chkSelectedStudent)
> chkSelectedStudent.ID = "chkSelectedStudent_" &
gridCell1.Text
> chkSelectedStudent.Value = gridCell1.Text
> tblRow.Cells.Add(tblCell0)
> tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
> tblRow.Cells.Add(tblCell1)
> tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
> tblRow.Cells.Add(tblCell2)
> tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
> tblRow.Cells.Add(tblCell3)
> tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
> tblRow.Cells.Add(tblCell4)
> Table1.Rows.Add(tblRow)
> End If
> Next
> End Sub
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Label1.Text = DateTime.Now.ToString
> If Page.IsPostBack Then
> AddToList()
> End If
> End Sub

Monday, March 26, 2012

Taborder after postback

After an autopostback event the control that gets focus next
is not the one defined in the taborder, but the first control.
This makes sense becuase the page is reloaded, but of course this
is not the desired behaviour.
I have seen the use of a hidden field, which stores the name of the control
that should get focus next, being set in the autopostback event, then an
OnPageLoad javascript that sets the focus to it.
This works, but only for 'forward' tabs, and the next control has nothing
to do with the taborder but is set in code.
It seems like there should be a better solution to this problem.
Actually, it seems like the viewstate should handle this automatically.
Any ideas or solutions?
kpg"kpg" <no@.way.com> wrote in message
news:Xns97CE93E88E40Cipostthereforeiam@.2
07.46.248.16...

> It seems like there should be a better solution to this problem.
There doesn't need to be, as it's fully customisable server-side.

> Actually, it seems like the viewstate should handle this automatically.
ViewState simply preserves values in form field either side of a postback
until / unless they are changed by that postback - no more, no less.

> Any ideas or solutions?
You already know which of your client-side objects (button, dropdownlist,
hyperlink, checkbox etc) has caused the postback, and write server-side code
to be processed accordingly. Simply add the following to the end of the
server-side code as required:
ClientScript.RegisterStartupScript(this.GetType(), "focus",
"<script>document.getElementById('...').focus();</script>");
Obviously, replace ... with the ID of the control that you want to receive
the focus.
N.B. this is v2 syntax, but v1.1 is almost identical.
As Mark Rae once said in microsoft.public.dotnet.framework.aspnet

> ClientScript.RegisterStartupScript(this.GetType(), "focus",
> "<script>document.getElementById('...').focus();</script>");
Well, that is a more eloquent solution than the one I had
described and I thank you for it.
It does still leave the (albeit small) issue of the user
pressing a shift-tab on an autopostback field. In that
case it would still move forward in the tab order.
And not to dwell on this point to much, my comment about
the viewstate was to simply introduce the concept that
the page 'knows' where it would have set focus to before
the postback, and as such it could remember this bit of
information somewhere (the viewstate for lack of a better
place?) and automatically move the focus there when the page
is returned. Perhaps there are reasons why this can't be
done, it was just an intuitive thought I had.
But as it stands I'll use your suggestion.
Thanks.
kpg
"kpg" <no@.way.com> wrote in message
news:Xns97CEA8488D4E8ipostthereforeiam@.2
07.46.248.16...

> It does still leave the (albeit small) issue of the user
> pressing a shift-tab on an autopostback field. In that
> case it would still move forward in the tab order.
Hmm - yeah, OK, I know what you mean but as you hint at, the likelihood of
this happening is fairly small. I've always been a great believer in the
80/20 rule for web development i.e. 20% of effort supplies 80% of usability,
and just how far you go to supplying the final 20% is really down to
time/budget/sanity...

> And not to dwell on this point to much, my comment about
> the viewstate was to simply introduce the concept that
> the page 'knows' where it would have set focus to before
> the postback, and as such it could remember this bit of
> information somewhere (the viewstate for lack of a better
> place?) and automatically move the focus there when the page
> is returned. Perhaps there are reasons why this can't be
> done, it was just an intuitive thought I had.
No, the ViewState really doesn't know this - that's not what it's for.
The ViewState's one and only purpose is to persist form values and page
variables across postbacks.
You as the programmer know what's supposed to happen next, so go to it -
that's your job! :-)
As Mark Rae once said in microsoft.public.dotnet.framework.aspnet

> You as the programmer know what's supposed to happen next, so go to it
> - that's your job! :-)
Point taken, but of course languages do indeed provide much of
what programmers used to do. Case in point - tab order. In
DOS programming (I know, I go way back) the programmer had to
specify all possible movements based on left, right, up, down,
tab, CR. Then in windows programming, tab order was introduced,
and the run time system handled movement. Well, all that spoiled
me, so with web programming (which I'm new to, can you tell?) I
see many areas where we are taking a step backwards.
Well, I've ridden this point well into the ground so I'll stop.
Thanks for your help.
kpg

Taborder after postback

After an autopostback event the control that gets focus next
is not the one defined in the taborder, but the first control.

This makes sense becuase the page is reloaded, but of course this
is not the desired behaviour.

I have seen the use of a hidden field, which stores the name of the control
that should get focus next, being set in the autopostback event, then an
OnPageLoad javascript that sets the focus to it.

This works, but only for 'forward' tabs, and the next control has nothing
to do with the taborder but is set in code.

It seems like there should be a better solution to this problem.

Actually, it seems like the viewstate should handle this automatically.

Any ideas or solutions?

kpg"kpg" <no@.way.com> wrote in message
news:Xns97CE93E88E40Cipostthereforeiam@.207.46.248. 16...

> It seems like there should be a better solution to this problem.

There doesn't need to be, as it's fully customisable server-side.

> Actually, it seems like the viewstate should handle this automatically.

ViewState simply preserves values in form field either side of a postback
until / unless they are changed by that postback - no more, no less.

> Any ideas or solutions?

You already know which of your client-side objects (button, dropdownlist,
hyperlink, checkbox etc) has caused the postback, and write server-side code
to be processed accordingly. Simply add the following to the end of the
server-side code as required:

ClientScript.RegisterStartupScript(this.GetType(), "focus",
"<script>document.getElementById('...').focus();</script>");

Obviously, replace ... with the ID of the control that you want to receive
the focus.

N.B. this is v2 syntax, but v1.1 is almost identical.
As Mark Rae once said in microsoft.public.dotnet.framework.aspnet

> ClientScript.RegisterStartupScript(this.GetType(), "focus",
> "<script>document.getElementById('...').focus();</script>");

Well, that is a more eloquent solution than the one I had
described and I thank you for it.

It does still leave the (albeit small) issue of the user
pressing a shift-tab on an autopostback field. In that
case it would still move forward in the tab order.

And not to dwell on this point to much, my comment about
the viewstate was to simply introduce the concept that
the page 'knows' where it would have set focus to before
the postback, and as such it could remember this bit of
information somewhere (the viewstate for lack of a better
place?) and automatically move the focus there when the page
is returned. Perhaps there are reasons why this can't be
done, it was just an intuitive thought I had.

But as it stands I'll use your suggestion.

Thanks.
kpg
"kpg" <no@.way.com> wrote in message
news:Xns97CEA8488D4E8ipostthereforeiam@.207.46.248. 16...

> It does still leave the (albeit small) issue of the user
> pressing a shift-tab on an autopostback field. In that
> case it would still move forward in the tab order.

Hmm - yeah, OK, I know what you mean but as you hint at, the likelihood of
this happening is fairly small. I've always been a great believer in the
80/20 rule for web development i.e. 20% of effort supplies 80% of usability,
and just how far you go to supplying the final 20% is really down to
time/budget/sanity...

> And not to dwell on this point to much, my comment about
> the viewstate was to simply introduce the concept that
> the page 'knows' where it would have set focus to before
> the postback, and as such it could remember this bit of
> information somewhere (the viewstate for lack of a better
> place?) and automatically move the focus there when the page
> is returned. Perhaps there are reasons why this can't be
> done, it was just an intuitive thought I had.

No, the ViewState really doesn't know this - that's not what it's for.

The ViewState's one and only purpose is to persist form values and page
variables across postbacks.

You as the programmer know what's supposed to happen next, so go to it -
that's your job! :-)
As Mark Rae once said in microsoft.public.dotnet.framework.aspnet

> You as the programmer know what's supposed to happen next, so go to it
> - that's your job! :-)

Point taken, but of course languages do indeed provide much of
what programmers used to do. Case in point - tab order. In
DOS programming (I know, I go way back) the programmer had to
specify all possible movements based on left, right, up, down,
tab, CR. Then in windows programming, tab order was introduced,
and the run time system handled movement. Well, all that spoiled
me, so with web programming (which I'm new to, can you tell?) I
see many areas where we are taking a step backwards.

Well, I've ridden this point well into the ground so I'll stop.

Thanks for your help.

kpg

Saturday, March 24, 2012

Tabview control focus problem

Hi,

I have a yahoo tabview control with 3 tabs in my aspx page. If I am in the third tab and a control fires a postback, then I need the page to load with the third tab in focus. Instead it is showing the default first tab.

Any help is appreciated.

Thanks.

Hello,

Because we don't know how to code the third-party control, you may find some codes like this:

<script type="text/javascript">

var myTabs = new YAHOO.widget.TabView('demo', { activeIndex: 1 } ); // make tab at position 1 active

alert(myTabs.get('activeIndex')); // alerts 1

myTabs.set('activeIndex', 0); // make tab at index 0 active

alert(myTabs.get('activeIndex')); // alerts 0

</script>

Or

<li class="selected"><a href="#tab1"><em>Tab One Label</em></a></li>

As shown above, the red lines you see may help you, just change the index number you want.

There are more references for you:

A Simple TabView for Web Pages

http://www.codeproject.com/asp/TabView.asp

HTH


Hi could you let me know how to create tab forms in ASP.NET 2.0 without using any thirdparty tools...Thanks