How to get values of changed text - asp.net

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

Related

how to redirect to another page, that generates a gridview, after clicking a button in the first page

i have some different buttons in a page after clicking them i want them to redirect me to a page that has a gridview , but for each button it gives a different gridview which are generated by datatables . may you help to find ,how can i do that in asp.net, vb ??
i did two pages at the gridview page i made methods that are binding different gridviews for each button , and in the first page where i have the button click events i did the redirecting to this gridview page and called the corresponding methods. it redirects me but doen't give me any gridview at all :(
Call in button action
Response.Redirect("Your Page Name.aspx")
and call in that page a BindGrid Function that you create to bind the gridview
You can redirect by using
Response.Redirect("GridView.aspx")
At the same time you can use only one aspx by using session
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Session.Add("CheckButton", Button1Clicked)
Response.Redirect("GridView.aspx")
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Session.Add("CheckButton", Button2Clicked)
Response.Redirect("GridView.aspx")
End Sub
After that at your GridView.aspx on your pageload
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim CheckButton As String = Session("CheckButton")
Dim query As String
If (CheckButton = "Button1Clicked") Then
query = "SELECT * FROM Table1"
Else If (CheckButton = "Button2Clicked") Then
query = "SELECT * FROM Table2"
Else
Response.Write("<script>alert('Error!')</script>")
End If
SqlDataSource1.SelectCommand = query
SqlDataSource1.DataBind()
End Sub

How can do postback using response.redirect?

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.

Simple LinkButton Postback

This has got to be the simplest question ever. Im trying to use a link button with the "postbackurl" to post a value to another page. It works with a button, but it is blank with a linkbutton. I dont want to use querystring. Any ideas?
default.aspx:
<asp:LinkButton ID="LinkButton" postbackurl="~/2.aspx" Text="ThisIsATest" runat="server">ThisIsATest</asp:LinkButton>
2.aspx:
Dim test = Request.Form("LinkButton")
Response.Write(test)
Try with this code
var text = ((LinkButton)PreviousPage.FindControl("LinkButton")).Text;
Use PreviousPage.FindControl to get the control reference
Dim lb As LinkButton = DirectCast(PreviousPage.FindControl("LinkButton"), LinkButton)
If lb IsNot Nothing Then
Response.Write(test.ID) ' display linkbutton ID
Response.Write(test.Text) ' display linkbutton Text
End If
Try:
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton1.Click
Response.Redirect("2.aspx?id=" + somestring)
End Sub

VB.NET Checkboxes checking

I have a form which contains a checkbox field. On page load I want to create a separate checkbox for each customer in my Database. The code I have to create the checkboxes for each customer works fine. However, I also want to check in the database if the customer is set to unauthorized if they are then I want to check there box. I also have code for the case where the user checks a box. If a box is checked I update the database setting the unauthorized attribute to true. My problem is when I check a box it works fine and the box is checked, however if I reload the page all the boxes are unchecked. So either my database update is not updating the database or the way I check on page load for checked boxes is incorrect. Any ideas?
The code for the asp checkbox field:
<asp:CheckBoxList id="check1" AutoPostBack="True" TextAlign="Right" OnSelectedIndexChanged="Check" runat="server">
</asp:CheckBoxList>
The Code for the page load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sql As String = "SELECT Name, unauthorized, ID FROM Customer ORDER BY Name"
Dim dt As DataTable = db.execDataTableQuery(sql, "Customer")
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
check1.Items.Add(New ListItem(CStr(dt.Rows(i).Item("Name"))))
check1.Items(i).Value = CInt(dt.Rows(i).Item("ID"))
check1.Items(i).Text = CStr(dt.Rows(i).Item("Name"))
If CInt(dt.Rows(i).Item("unauthorized")) = 1 Then
check1.Items(i).Selected = 1
Else
check1.Items(i).Selected = 0
End If
Next
End Sub
The code to update the database:
Sub Check(ByVal sender As Object, ByVal e As EventArgs)
Dim sql As String
If check1.SelectedItem.Selected = 1 Then
sql = "UPDATE Customer SET unauthorized = 1 WHERE ID = #ID"
db.execUpdateQuery(sql, New SqlClient.SqlParameter("#ID", check1.SelectedItem.Value))
Else
sql = "UPDATE Customer SET unauthorized = 0 WHERE ID = #ID"
db.execUpdateQuery(sql, New SqlClient.SqlParameter("#ID", check1.SelectedItem.Value))
End If
End Sub
End Class
You need to wrap you code in Page_Load in a Not Page.IsPostback, otherwise on postback no events are triggered and the selection is lost since you're overwriting it from database.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostback Then
' databind your CheckBoxList '
End If
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