CommandArgument on SQLDatasource Inserted? - asp.net

I'm wishing to redirect to two different pages depending on which button is pressed on my form.
Both buttons call the Insert method of the Datasource, but only one of them needs to get hold of the newly inserted GUID and redirect using that GUID.
I'm doing it using this code
Protected Sub DSCustomers_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles DSCustomers.Inserted
Response.Redirect("~/addTicket.aspx?step=2&customerID=" & e.Command.Parameters("#customerID").Value.ToString)
End Sub
But how can I know if it's the other button that has been pressed and doesn't need to redirect?
I can't get access to the CommandArgument otherwise I would just check which button has been pressed using that and then redirect accordingly.
Thanks

You can't just use a global private boolean?
Dim RedirectButtonPressed As Boolean = false
Protected Sub RedirectButton_Click(sender As Object, e As EventArgs) Handles RedirectButton.Clicked
// code code code
RedirectButtonPressed = True
End Sub
Protected Sub DSCustomers_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs) Handles DSCustomers.Inserted
If RedirectButtonPressed Then
Response.Redirect("~/addTicket.aspx?step=2&customerID=" & e.Command.Parameters("#customerID").Value.ToString)
RedirectButtonPressed = False
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

creating event control in vb.net. Multiple controls with the same ID '1' were found

Im trying to create buttons on page load and have event controls to those. Im able to create the buttons but the event doesnt seem to be triggered when the button is clicked instead it throws an error stating multiple controls with id found.I think this has something to do with postback and unique ID creation for the buttons. can some one point me as to what to be added along with this?
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.ID = "but"
AddHandler but.Click, AddressOf Button_Click
Me.form1.Controls.Add(but)
End Sub
The event control for this is as below.
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Im getting the error
Multiple controls with the same ID '1' were found
The subroutine createbutton works on page load as follows.
Public Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' to gen page on load ;)
createbutton()
End Sub
Help is appreciated , Thanks :)
i think you are a guy who worked a lot with VB 6.0
Control array feature is available in VB 6.0
in VB.net everything is depend upon control ID.
if you don't have specific requirement like ID should be same then i suggest pls append prefix and incremental ID would resolve your issue.
Let me know if you have some specific requirements
Thanks
Asif
Corrected Code, Instead of ID it's name which allow uniqueness
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createbutton()
End Sub
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.Name = "but"
AddHandler but.Click, AddressOf Button_Click
Me.Controls.Add(but)
End Sub
End Class

How To Pass Control As A Variable

This is one of those problems which seems like it should have a simple solution but I can't work out what it is!
How can I pass a control from one sub to another if the first sub doesn't actually call the second? For example, where btnChangeText is in a panel that has a ModalPopupExtender called mpExample, and therefore isn't usually visible:
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
<SpecifiedTextBox>.Text = "Hello"
End Sub
And then on the main page, visible at all times, is a button associated with each textbox. In this example, it's textbox15:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
<Set TextBox15 as variable>
mpExample.Show()
End Sub
I know it's a silly example - believe me when I say that the real application I want to make of this actually makes sense! But the point is that I want to somehow store the name of the control to be updated by the first sub when the second sub is run.
If I was calling the first sub from the second it'd be easy, I'd just pass it as an argument, but I'm not. The first sub is called from a button click and is an independent action from the running of the second sub.
I don't seem to be able to use a session variable (my first thought) because I can't find any way to store the control name as a string and then convert it back to an actual control when the first sub runs. That'd be the easiest answer if somebody could tell me how to do it.
One approach would be to store the control's ID as a string in a Session variable, and then use the FindControl method to grab the control in your 2nd Click event.
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15.ID
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Page.FindControl(Session("currentTextBox")),TextBox)
currentTextBox.Text = "Hello"
End Sub
Note that if your TextBox15 control is inside some kind of container (a Panel or something), you'll need to use that container's FindControl method, rather than Page.FindControl.
Another approach is to store the TextBox itself in a Session variable, and then pull that out to set the text in your other method. Note that this only works if the methods are both called in the same request (which doesn't sound like it would work for your use-case). Here's what that would look like:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Session("currentTextBox"), TextBox)
currentTextBox.Text = "Hello"
End Sub

adding events to programatically added controls in web user control

I am trying to put a - what seems - very simple web user control
basically i want it to render as a dropdownlist/checkboxlist or radiolist based on a property
but also want to be able to work out what is selected
i was trying the following - but cant seem to work out how to attach to the selectedindexchanged of the listcontrol so that i can set the selectd value(s)
its not helping that my VB is not up to much but am forced to use it in this instance
its not even giving me the intellisense for the event..
Public Options As List(Of Options)
Public ControlRenderType As ControlRenderType
Public IncludeFreeOption As Boolean
Public SelectedOptions As List(Of Options)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim c As ListControl
Select Case (ControlRenderType)
Case STGLib.ControlRenderType.CheckBoxList
c = New CheckBoxList()
Case STGLib.ControlRenderType.DropdownList
c = New DropDownList()
Case STGLib.ControlRenderType.RadioButtonList
c = New RadioButtonList()
Case Else
Throw New Exception("No Render Type Specified")
End Select
For Each opt In Options
Dim li = New ListItem(opt.Description, opt.ID)
c.Items.Add(li)
Next
c.SelectedIndexChanged += ..?? or something
Page.Controls.Add(c)
End Sub
can anyone explain please - it is of course quite possible that I am going about this in completely the wrong way..
thanks
First create a Sub or a Function to handle the IndexChange of the object that you have created dynamically and make sure that the signature of the Sub is something like this
Sub myOwnSub(ByVal sender As Object, ByVal e As EventArgs)
...
... Handle your event here
...
End Sub
Then after you create your object add the following code
Dim obj as ListBox
AddHandler obj.SelectedIndexChanged, AddressOf myOwnSub

Send parameter to addhandler?

I have a button in a grid that I created programmatically. The button edits some data in a table using data in a hidden column of the grid that the button is in. Normally I send a hidden field the row data using javascript onclientclick of the button then make the changes to the database using that hidden field. But there must be a way to send the addhandler of the button a parameter. This is the code i have to clarify....
Dim btnedit As New ImageButton
AddHandler btnedit.Click, AddressOf btnedit_Click
btnedit.ImageUrl = "\images\bttnEditMini.gif"
If e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Footer Then
e.Row.Cells(3).Controls.Add(btnedit)
End If
here is my Addhandler with its delegate:
Public Delegate Sub ImageClickEventHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Sub btnedit_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
//programming stuff
End Sub
How can i send this handler a parameter?
By convention, all event handlers accept two parameters: the sender, and the EventArgs. If you need to send custom information to the listeners, create a new class that inherits from EventArgs and contains the information that you need to communicate.
Check out this article on CodeProject that shows you how to do this.
Short answer: no. Where would you send it? You've got two parameters.
Longer answer: sender is the control that sent the event. In this case, it will be your btnEdit control. Maybe that will help you.
Since it was in a grid i just used the row command instead. And when Row command is used you can send it a commandname and a commandargument. I passed my parameter as the argument.
GridView1.Rows(i).Cells(3).Controls.Add(btndel)
btndel.ImageUrl = "\images\bttnDelete.gif"
btndel.ToolTip = "This will delete the Selected Assignment"
btndel.CommandName = "destroy"
btndel.CommandArgument = GridView1.Rows(i).Cells(0).Text
btndel.Attributes.Add("onclick", "javascript: if(confirm('Are you sure you want to delete this Department Cost Days Assignment?')==false) return false;")
here is the rowcommand:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "destroy" Then 'used destroy because Delete command was prohibited.
Call Connection()
Dbcmd.CommandText = "Delete from table where condition = '" & e.CommandArgument & "'"
Dbcmd.ExecuteNonQuery()
Dbconn.Close()
Dbconn.Dispose()
End If
If you don't want to use the 'default'/already defined parameters, you can create your own events.

Resources