Simple LinkButton Postback - asp.net

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

Related

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

Creating a popup box on button click

and thank you for viewing my post. I have a project in vb completed in Visual Studio 2010. Once the user clicks the submit button, I want a popup message that has certain text. I also want an okay and cancel button. If the user clicks okay, the page goes as normal and submits. It cancel is clicked, the page goes back for the user to edit info.
Now, I've tried using msgbox, but it doesn't work server side. From research, I found I prob need to use Jquery. I don't have much experience with that, so any help is appreciated!
Thanks,
Josh
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If (Not Page.IsPostBack) Then
Me.btnSubmit.Attributes.Add("onclick", "return confirm('Please note that what you have entered will be used to auto-generate the flyer. Also, submissions less than 30 days from the date entered may be denied.');")
End If
Dim objRequestID As Object = Nothing
Dim intRequestID As Integer = 0
Try
If ValidateForm() Then
Using objConnection As SqlConnection = util.GetConnection()
Using objTransaction As SqlTransaction = objConnection.BeginTransaction
Using objCommand As SqlCommand = New SqlCommand("Request_Insert", objConnection)
With objCommand
.CommandType = Data.CommandType.StoredProcedure
.Transaction = objTransaction
.Parameters.Add("#CourseTypeID", Data.SqlDbType.Int).Value = util.CourseRequestType.PIE
.Parameters.Add("#SponsorName", Data.SqlDbType.NVarChar, 50).Value = SponsorName.Text.Trim
.Parameters.Add("#SponsorPhone", Data.SqlDbType.NVarChar, 20).Value = SponsorPhone.Text.Trimenter code here
You need to put client button onclick event at server Page_Load event.
When you reach to btnSubmit_Click, it is too late.
Protected Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
If (Not Page.IsPostBack) Then
Me.btnSubmit.Attributes.Add("onclick", "return confirm('Please...');")
End If
End Sub
you can call this javascript on button click
<script>
function myFunction()
{
alert("I am an alert box!");
}
</script>

button add event click event

I have a button within a formview control on my page.
Because the button is contained within the formview, my code-behind can't see it.
So I did this:
Dim btnSave As Button = CType(fvCourse.FindControl("btnSave"), Button)
And then I added an event handler like this:
AddHandler btnSave.Click, AddressOf btnSave_Click
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.write("hey!")
End Sub
The problem is, I don't think it's working because I never see the "hey!" on my page.
Am I missing something?
Thanks
I don't know about missing something, but I reckon you could do it a simpler way since you're using VB. Give your button a command name and command argument first:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
CommandArgument="1" CommandName="yes" />
These can be anything - typically you use the command name to determine which button a user clicked on, and the command argument to show the record id.
In your code-behind, attach a macro to the ItemCommand event of the FormView (which fires when something happens within it):
Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
Select Case e.CommandName.ToLower
Case "yes"
'test
Label2.Text = "You chose " & e.CommandArgument.ToString
End Select
End Sub Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
Select Case e.CommandName.ToLower
Case "yes"
'test
Label2.Text = "You chose " & e.CommandArgument.ToString
End Select
End Sub
And in VB, that's all you need to do!
You should use the ItemCreated event of the FormView for such things. If the Button is in the ItemTemplate you need to check for the FormViewMode.ReadOnly, for EditItemTemplate you need to use Edit:
Private Sub fvCourse_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvCourse.ItemCreated
Select Case fvCourse.CurrentMode
Case FormViewMode.Edit
Dim btnSave As Button = DirectCast(fvCourse.FindControl("btnSave"), Button)
AddHandler btnSave.Click, AddressOf btnSave_Click
End Select
End Sub

Deleting Dynamically Populated images from a Directory (NOT GridView) ASP.NET (VB)

The code below displays a thumbnail of each image in a specific server directory and when I click on the image it pops up a nice full sized picture. It works perfectly.
I would however, like to be able to delete an image. I first thought I could have a button at the bottom of the page with a checkbox next to each image, giving it a uniqueID as per the filename but as they are dynamically created I couldn’t figure how to handle the Click Event on the button for a randomly named Checkbox ID. Then I tried adding a button next to each item and then tried an OnClick & OnServerClick to call a Sub but this didn’t work either.
Any/All suggestions welcomed :)
Private Sub ImageList()
If Directory.Exists(Server.MapPath("JobImages\" & DBC_JOB_JobID.Text)) Then
Dim MySB As New StringBuilder
Dim dirInfo As New DirectoryInfo(Server.MapPath("JobImages\" & DBC_JOB_JobID.Text))
MySB.Append("<ul class=""clearfix"">")
MySB.AppendLine()
For Each File In dirInfo.GetFiles()
MySB.Append("<li><a rel=""jobpic"" href=""JobImages\" & DBC_JOB_JobID.Text & "\" & File.Name & """><img src=""JobImages\" & DBC_JOB_JobID.Text & "\Thumbs\" & File.Name & """ width=""150"" height=""100"" /> <span class=""size"">" & File.Name & " </span></a></li>")
MySB.AppendLine()
Next
MySB.Append("</ul>")
MySB.AppendLine()
lblMyPictures.Text = MySB.ToString
End If
End Sub
OK what Kendrick is talking about (basically) is using server side controls to keep track of which file to delete. What you are doing right now is dumping markup into a Label control, which on postback won't fire an event on the server side. However you can accomplish this easily with server side controls.
The basic idea is you use a container control such as a Panel and add each child control to it. Then you hook events to each row with data identifying that row (such as filename).
Markup:
<asp:Panel ID="pnlList" runat="server">
</asp:Panel>
Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Directory.Exists(Server.MapPath("Files")) Then
Dim objDirInfo As New DirectoryInfo(Server.MapPath("Files"))
For Each objFile As FileInfo In objDirInfo.GetFiles()
Dim objLabel As New Label
objLabel.Text = objFile.Name
Dim objLinkButton As New LinkButton
objLinkButton.Text = "Delete"
objLinkButton.CommandName = "Delete"
objLinkButton.CommandArgument = objFile.Name
AddHandler objLinkButton.Command, AddressOf DeleteFile
Dim objLiteral As New LiteralControl
objLiteral.Text = "<br/>"
pnlList.Controls.Add(objLabel)
pnlList.Controls.Add(objLinkButton)
pnlList.Controls.Add(objLiteral)
Next
End If
End Sub
Public Sub DeleteFile(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
If e.CommandName = "Delete" Then
Dim strFileName As String = Server.MapPath("Files\" & e.CommandArgument)
If File.Exists(strFileName) Then
Dim objFile As New FileInfo(strFileName)
objFile.Delete()
End If
End If
End Sub
This would be an excellent example of where using a data aware would make your life a lot easier.
That said, if you didn't want to use a server-side control, you could assign an ID to each checkbox (i.e. DeleteImage_1) and then store the ID and associated image name in the viewstate on the page. Go through the checked checkboxes and refer back to the viewstate for the name that goes with each ID when they click the delete button.

How can i scroll to an anchor?

I have bound a datapager control to a listview.
I would like to scroll to the first item of the listview control on the DataPager click. I think this should be done with javascript. It seems that the datapager does not allow that.
What options do I have? How can I scroll to a specific anchor when clicking on the DataPager?
you can use the basic html named anchor to scroll to a specific anchor.
You can use the javascript function scrollIntoView for that on client side or on "server side":
http://www.codeproject.com/KB/aspnet/ViewControl.aspx
Thanks Tim!
And for the lazy guys out there (just like me ;), here is the VB.NET equivalent.
It contains typo corrections and the new RegisterClientScriptBlock Method
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FocusControlOnPageLoad("Label1", Me.Page)
End Sub
Public Sub FocusControlOnPageLoad(ByVal ClientID As String, ByVal page As System.Web.UI.Page)
Dim csName As String = "ScrollViewScript"
Dim csType As Type = Me.GetType
Dim cs As ClientScriptManager = page.ClientScript
If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
Dim csText As New StringBuilder()
csText.Append("<script>function ScrollView(){")
csText.Append("var el = document.getElementById('" & ClientID & "');")
csText.Append("if (el != null){")
csText.Append("el.scrollIntoView();")
csText.Append("el.focus();}}")
csText.Append("window.onload = ScrollView;")
csText.Append("</script>")
cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
End If
End Sub

Resources