How can do postback using response.redirect? - asp.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' hdncurrpayperiod.Value = test1.ToString
hdnPayperiod.Value = test1.ToString
lblPayPeriodStartDt.Text = test1
Else
lblPayPeriodStartDt.Text = hdnPayperiod.Value
End If
End Sub
Private Sub btnnext_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnnext.Click
' hdnPayPeriodStartDt.Value = ""
Dim datenext As Date
Dim datenextpayperiod As String
datenext = DateTime.Parse(lblPayPeriodStartDt.Text)
datenextpayperiod = datenext.Add(New TimeSpan(14, 0, 0, 0)).ToString("MM/dd/yyyy")
hdnPayperiod.Value = datenextpayperiod
lblPayPeriodStartDt.Text = hdnPayperiod.Value
'hdnPayperiod.Value = lblPayPeriodStartDt.Text.ToString
Response.Redirect("TimeSystem.aspx?PayPeriodStartDate=" + HttpUtility.UrlEncode(hdnPayperiod.Value), False)
End Sub
This is a user control which has calender with next and previous buttons and a label. I get the initial date from the a user control property on initial load(not postback) and on next button click it adds 14 days and displays on label. Everything is fine until I use response.redirect which is hitting not postback and getting the property value again. How can I avoid this. I have to pass the value of date with 14 days added to it in the url but this happens only once as it is refreshing with property value every time It hits response.redirect. Please let me know if I am not clear

In Page_Load in Not IsPostBack block add another 'if' statement that checks if 'PayPeriodStartDate' is present in query string (Request.QueryString). If it is - use that value to set hidden field and label if it isn't use 'test1' value as in your code sample.

Related

Programmatically databind listview while using datapager

I have a listview and two data pagers. My listview is hooked up to a data source whose data is ordered randomly. ORDER BY NEWID()
As you can imagine, each time I select a page or click next/prev page all the data is randomized making the datapager quite useless.
I figured I could set the datasource when the page is not posted back and then programmatically set the datasource to the listview but now when selecting a page or clicking the next/prev buttons, the page just simply doesn't change..
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
lvListMembres.DataSource = dv
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End If
End Sub
What am I missing / is there a better way to do this. The order by MUST be random and I MUST have the data pagers separate from the listview.
Thank you!
Take the code between the If statement and place it in a separate function.
Add a method of storing the data, in this particular case I'll store it as a DataTable in a session.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Clear datatable from session
Session("CourtierList_dt") = Nothing
' Set datasource to listview and save in a session variable
SetCourtierList()
End If
End Sub
Protected Sub SetCourtierList()
Dim dt As DataTable
If Session("CourtierList_dt") Is Nothing Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
Session("CourtierList_dt") = dv.ToTable
End If
dt = Session("CourtierList_dt")
lvListMembres.DataSource = dt
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End Sub
Also, make sure to add the PagePropertiesChanging event to the listview
Protected Sub lvListMembres_PagePropertiesChanging(sender As Object, e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles lvListMembres.PagePropertiesChanging
DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
DataPager3.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
SetCourtierList()
End Sub
And there you have it, hope this is helpful for someone.

how to get the text value of dynamically created textbox

I want to get the value of my textbox that I created dynamically when I click a button
I need to do this cause the value of my textbox is used for retrieve data from database
how could I achieved this thing??
the flow is Button click - creating textbox - filling textbox with value - Button Click - Get Text of textbox
here is my code to make the textbox
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 4
textbox = New TextBox With {.ID = "TextBox" & i}
plchld.Controls.Add(textbox)
Next
End Sub
I have tried something like this but the code didn't work
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
thanks in advance for any help
edit for the answer
I've found the way to solve this. I use request.form to get the value of my textbox.
Thanks for anyone that participating
Regards,
Julian
This is how I have done in my asp.net application.
Creating dynamic control
TextBox txtDate = new TextBox();
txtDate.EnableViewState = true;
txtDate.ID = "PreixValue" + 1;
txtDate.Text = "07 Feb 2014"
pnl.controls.add(txtdate);
To retrieve the value from that textbox
DateTime datefrom = DateTime.Now ;
for (int cnt = 0; cnt < Request.Form.Count; cnt++)
{
if (Request.Form.AllKeys[cnt].Contains("Prefixvalue"))
{
int ParamStartPoint = Request.Form.AllKeys[cnt].IndexOf("Prefix") + 4;
int ParamNameLength = Request.Form.AllKeys[cnt].Length - ParamStartPoint;
string[] ControlName = Request.Form.AllKeys[cnt].Substring(ParamStartPoint, ParamNameLength).Split('$');
if (ControlName[0] == "Date From")
{
datefrom = DateTime.Parse(Request.Form[cnt]);
//datefrom has value now
}
}
}
This is how I have done in my web application, but there may be other ways achieve this.
basically when you create Dynamic control in webform this will be available through Request.Form.
hope this helps you.
Protected Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
Dim a(5) As String
For i As Integer = 0 To 4
Dim anotherObj As TextBox = Me.Controls.Item("Textbox" & i)
a(i) =anotherObj.Text
Next
The issue is that dynamic controls are lost on a postback so when the OkButton click event is handled, there is nothing inside your plchld control. You must recreate your controls with the same ID on postback if you want to retrieve the text in the textboxes.
Using your code, all you need to do is on postback determine if the textboxes were created and if so, recreate them.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Determine if the text boxes were created and if so, recreate them.
If CBool(ViewState("TextBoxesCreated")) Then
CreateTextBoxes()
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
CreateTextBoxes()
ViewState("TextBoxesCreated") = True
End Sub
Private Sub CreateTextBoxes()
For i As Integer = 0 To 4
plchld.Controls.Add(New TextBox With {.ID = "TextBox" & i})
Next
End Sub
Protected Sub OkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OkButton.Click
Dim a(4) As String
For i As Integer = 0 To 4
a(i) = CType(plchld.FindControl("Textbox" & i), TextBox).Text
Next
End Sub
I don't know the full extent of what you are doing but I would suggest not creating them dynamically if you don't need to. Just show or hide the textboxes instead.
Reference: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

How to get values of changed text

When I click on the link in a GridView, it will redirect me to another page and pass a parameter at the same time.
code is as shown at below.
<ItemTemplate>
<asp:LinkButton ID="EditAnnouncement" runat="server" CommandName="Edit" CommandArgument='<%# Bind("annID") %>'>Edit</asp:LinkButton>
</ItemTemplate>
This is the code in vb
Response.Redirect("editmemannouncement.aspx?annID=" + e.CommandArgument)
This is the directed page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
param1 = Request.QueryString("annID")
//search record using the "annID" (will only get 1 record)
TextBox2.Text = reader.Item("anntitle").ToString
User may change the text in TextBox2. In the directed page, there is also a button. When I click on the button, I want to get the changed text in TextBox2. I tried
Dim s As String = TextBox2.Text
but I only get back the original value instead of the changed value. How can I get the changed value from TextBox2.Text
On every page load you set again the TextBox2, so even if you do have change it, you do not see the changes because you overwrite it.
Use the IsPostBack to not overwrite it when you make post back as:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
param1 = Request.QueryString("annID")
//search record using the "annID" (will only get 1 record)
TextBox2.Text = reader.Item("anntitle").ToString
End If
You need to keep your code inside if(!Page.IsPostBack) section ,
If Not IsPostBack
param1 = Request.QueryString("annID")
//search record using the "annID" (will only get 1 record)
TextBox2.Text = reader.Item("anntitle").ToString
Try to use
IsPostBack : Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.
Sub Page_Load
If Not IsPostBack
param1 = Request.QueryString("annID")
TextBox2.Text = reader.Item("anntitle").ToString
End If
End Sub

Items.Count returning 0 for a list box

I am trying to use the code below to store the items from the list into a session. For some reason when I debug the code the count is returning 0 even though there are multiple items in the list box? Any ideas what I am doing wrong here?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
NameTextBox_AutoCompleteExtender.OnClientItemSelected = "getSelected"
End Sub
Protected Sub cmdNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles cmdNext.Click
Dim n As Integer = NPListbox.Items.Count
Dim arr As String() = New String(n - 1) {}
For i As Integer = 0 To arr.Length - 1
arr(i) = NPListbox.Items(i).ToString()
Next
Session("arr") = arr
Response.Redirect("~/frmDescription.aspx")
End Sub
<script language="javascript" type="text/javascript">
function getSelected(source, eventArgs) {
var s = $get("<%=NameTextBox.ClientID %>").value;
var opt = document.createElement("option");
opt.text = s.substring(s.length - 10);
opt.value = s.substring(s.length - 10);
document.getElementById('<%= NPListbox.ClientID %>').options.add(opt);
}
I am going to guess that you do not have any logic in your Page_Load to populate the listbox, based upon what it had when you finished the autocomplete extender logic. Since, you do not, then when the click event fires after the Page_Load your values are gone.
Put the logic that executes on selection of the autocomplete extender in a method and have your Page_Load call that, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End Sub
Private Sub PopulateListBox()
' Go to whatever resource you are using to get the values for the list box
End Sub
UPDATE:
Since you are depending upon using a client-side function to grab the values from the autocomplete extender and populating the listbox that way, you need to mimic that logic in your Page_Load on the server-side, because it will be too late if you try to use the client-side one, since you need the data server-side and all of the server-side events happen before the client-side logic in a server post back.
You need to do something like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Only do this when page has posted back to the server, not the first load of the page
If IsPostBack Then
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End If
End Sub
Private Sub PopulateListBox()
' Get value from text box
Dim textBoxValue As String = Me.NameTextBox.Text
' Create new item to add to list box
Dim newItem As New ListItem(textBoxValue)
' Add item to list box and set selected index
NPListbox.Items.Add(newItem)
NPListbox.SelectedIndex = NPListbox.Items.Count - 1
End Sub

problem with asp textbox control

i have a textbox control in asp.net. textbox have a search button beside it. On clicking the search button i redirect to new page with the value in textbox. the new page also hase textbox and button beside it. I set the value sent from previous page to the textbox on new page. If i change the value on new page and click the search button it should take the new value. But it takes the previous value.
on page load method wrote the following code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End Sub
On button click following code
Protected Sub bsrcnew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsrcnew.Click
Dim s As String
s = txtsrch.Text
If (flag.Equals(0)) Then
Response.Redirect("newSearch.aspx?str1=" + s)
ElseIf (flag.Equals(1)) Then
Response.Redirect("termsnew.aspx?str1=" + s)
End If
end sub
can any1 tell me how do i get changed value in textbox?
Try assigning it in a
If(!IsPostBack)
{
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
}
On buttonclick its again assigning the value from query string.
Use IsPostBack to set your text box only on the first run in the second page. Example:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End If
End Sub

Resources