I have a dropdownlist with autopostback and an update panel, it works correctly,
but I have also other controls in my page with autopostback.
What I need is to control when the page is autopostback but the dropdownlist is not autopostback do something, like this:
If is not Page.autopostback then
'do something
else if is not MyDropdownlist.autopostback then
' do something different
End if
I can use this:
If is not Page.autopostback then
End If
But I canĀ“t do this:
If is not MyDropdownlist.autopostback then
End If
So how can I do this? I hope my explanation has been helpful, thanks.
The __EVENTTARGET request form variable has the name of control that caused postback to happen. You can query the name of this control and do whatever you want to do.
For example,
If IsPostBack Then
Dim postBackControlId As String = Request.Form("__EVENTTARGET")
If Not String.IsNullOrEmpty(postBackControlId) Then
If postBackControlId = "DropdownList1" Then
' the postback happened due to DropdownList1
Else
' the postback happened due to some other control.
End If
End If
End If
Related
To explain my situation...
I am filling a listbox from a mysql table liek this
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim cmdtext = "SELECT * FROM avail_workouts"
Using conn = New MySqlConnection(connString)
Using cmd = New MySqlCommand(cmdtext, conn)
conn.Open()
reader = cmd.ExecuteReader()
While reader.Read()
ListBox1.Items.Add(reader("workout"))
End While
End Using
End Using
End Sub
I then select one of the items on the listbox and click on a button (that will do something with the value selected from the listbox) that does nothing for now. That's when I get this error
Invalid postback or callback argument.
Event validation is enabled using <pages enableEventValidation="true"/>
in configuration or <%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that
arguments to postback or callback events originate
from the server control that originally rendered them.
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation
method in order to register the postback or callback data for validation.
I tried using
EnableEventValidation="false"
This seemed to work, until I tried to use the selected value in the listbox. It seems to forget the value that is selected when I click on the button. So, how can I simply fill up a listbox, select a row in the listbox, and click a button where the selected value will be used without getting this error?
Thanks in advance!
include this lines...
If Not IsPostBack
//your code
End If
on textbox blank, i wanted to clear my gridview source
But i was not able to do it in vb.net.
After referring several answers i tried following unsuccessfull attempts:
grdUsers.rows.clear() : Does not work with vb.net
grdUsers.DataSource=""
grdUsers.columns.clear()
But it does not worked out.
Please help me to clear my datasource of gridview.
If your DataGridView is bound to a DataSource and you want to clear it then you can use the Nothing keyword followed by a DataBind().
grdUsers.DataSource = Nothing
grdUsers.DataBind()
Here is more information on the DataBind() method.
If you want to clear your rows when the text is empty in TextBox1, you would create a TextChanged event for your textbox ...
Private Sub TextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Trim = "" Then
grdUsers.DataSource = Nothing
grdUsers.DataBind()
End If
End Sub
I have four textboxes within an UpdatePanel, and AutoPostBack is set to True for all of them. When the user enters a value and hits Enter, I want the cursor to move to the next textbox automatically. When I don't have the controls in an UpdatePanel, the standard textbox.focus method works fine.
I found some code here that led me to create this:
Protected Sub txtField_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtField1.TextChanged,
txtField2.TextChanged, txtField3.TextChanged
'Based on the textbox where data was changed, move the cursor to the next field.
Try
Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
'As multiple textboxes fire this same event, determine which textbox triggered this.
Dim MyTextbox As TextBox = TryCast(sender, TextBox)
Select Case MyTextbox.ID.ToString
Case "txtField1"
sm.SetFocus(txtField2)
Case "txtField2"
sm.SetFocus(txtField3)
Case "txtField3"
sm.SetFocus(txtField4)
End Select
Catch ex As Exception
lblError.Text = "Error in [txtField_TextChanged]: " & ex.Message
End Try
End Sub
What is really strange is that this works ONCE on whatever field I try first. After that, the event is fired, but the focus does not change. Is there something additional I need to to for subsequent calls?
I would appreciate any suggestions. Thank you!
Try setting the EnablePartialRendering property of the ScriptManager to false, like so:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false"></asp:ScriptManager>
I hope this helps! Good luck, and happy coding :)
I have the following code to try to use viewstate to save a variable for postback. When post back occurs the SrString value is nothing. I have set the ViewState value at the dropdownlist index chane event and set the variable equal ot ViewState("SrString") at page in the if ispostback block.
Can anyone help?
Thanks
'Page Load
If IsPostBack Then
SrString = ViewState("SrString")
End If
'DropDownList Index change event
Protected Sub ByteDataFile_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ByteDataFile.SelectedIndexChanged
ViewState("SrString") = SrString
End Sub
My web config file is correct, because I have other pages in the web site that works with the viewstate fine.
What am I missing?
That is because Page_Load is executed before ByteDataFile_SelectedIndexChanged.
ASP.NET Page life cycle always executes Page_Load first and then it handles events like clicks and SelectedIndexChanged, so when you say SrString = ViewState("SrString") in Page_Load, the ViewState("SrString") = SrString line hasn't been called yet.
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Assuming you get the value for SrString from dropDownList's selected item, you just need to get that in Page_Load, something similar to this:
If IsPostBack Then
'I got this from your comment in the other answer, but I suppose LineNo and FileNameID are comming somehow from the drop downlist, right?
ViewState("SrString") = "\\...\soi\Bytewise\Line " & LineNo & "\Text Files\" & FileNameID
End If
Another thing you need to be sure is that the DropDownList has its AutoPostBack property set to true, otherwise the page won't postback when you change the selection.
For this kind of thing I think you should use a HiddenField instead of the ViewState.
http://wiki.asp.net/page.aspx/298/hiddenfield/
Where is SrString set? From the code you posted, it is only ever assigned to or from the ViewState, and therefore will always be null.
Some more explanation:
If IsPostBack Then
SrString = ViewState("SrString")
End If
'DropDownList Index change event
Protected Sub ByteDataFile_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ByteDataFile.SelectedIndexChanged
ViewState("SrString") = SrString
End Sub
In page load, we set SrString to the value in the view state.
In your changed event, we set the ViewState to the value of SrString.
However, at no time is SrString ever set to a value, so you're just passing a null value around. There needs to be, somewhere:
SrString = 'some value from somewhere besides the viewstate.
Given:
a=b
b=a
And no other assignment, the value will never change.
I have GridView control in my asp.net page with auto generated fields
there is only footer template is present under asp:TemplateField.
I can bind this gridview with any of databae table as depend on user
selection. Now want to add new record in database so I have added text
boxes on footer template cells at runtime depend on number of columns
on table. But when I accessing these text boxes from footer template
on gridview_RowCommand event its not retriving textbox control.
this is the code:
SGridView.ShowFooter = True
For i As Integer = 0 To ctrList.Count
Dim ctr As Control = CType(ctrList.Item(i), Control)
SGridView.FooterRow.Cells(i + 1).Controls.Add(ctr)
Next
the ctrList contain Controls textBox, checkbox dropdowlist ect.
there is all ok
but when i wont to get the text or value or checked value of controls i can't cast the controls in rowcommand event
Here is the code:
If e.CommandName = "Add" Then
Dim ctrList As ControlCollection = SGridView.FooterRow.Controls
For Each ctr As Control In ctrList
If TypeOf ctr Is TextBox Then
Dim name As TextBox = CType(ctr, TextBox)
Dim val As String = name.Text
End If
Next
End If
this excample is for the textBox control.
Plese suggest me how can I get footer control textboxes. So that I can
save data in database.
When you are dealing with Dynamic controls you have to keep a close eye on the .net page lifecycle.
Control data is bound in the Load event, so your not able to access postback data in that event when your doing your conrol generation in the Load event too. I usualy try to create dynamic controls in the Init of the page, and do any values processing on the LoadComplete or PreRender event so I can make sure they have recieved their values from the postback before you try to read them.
Take a look at the full description of the ASP.NET page lifecycle events and whats going on. This should help you navigate the creation and use of Dynamic generated controls.
Try to create your Controls in the Grdiview's RowCreated Event which will be raised on every Postback.
Private Sub Grid1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grid1.RowCreated
Select Case e.Row.RowType
Case DataControlRowType.Footer
'add controls to row
End Select
End Sub