How do I fill a form using a modal popup? - asp.net

I have an asp.net address form with a bunch of text boxes (street, number, city, postal code, etc.) and an AJAX modal-popup-extender.
When a button is pressed, an address-book is shown (in the modal popup) where the user can search and select one of his registered addresses. This list is implemented using a GridView and works very well. Each row has a link-button like this:
<asp:LinkButton ID="AddressNameLinkButton" runat="server"
CausesValidation="false"
CommandArgument='<%# Container.DataItem("ADDRESS_ID") %>'
CommandName="Select"
Text='<%# Eval("ADDRESS_NAME")%>' />
When the button is pressed, this code-behind runs:
Protected Sub AddressGridView_RowCommand(...) Handles AddressGridView.RowCommand
Dim ADDRESS_ID As Long = e.CommandArgument
If e.CommandName.CompareTo("Select") = 0 Then
'Some code that retrieves the information
'of the selected address goes here.
CityTextBox.Text = City
PostalCodeTextBox.Text = PostalCode
'Etc. Etc.
SearchModalPopupExtender.Hide()
End If
End Sub
Problem is, while this code hides the modal-popup, it does not fill the address form.
How do I fill the form from the modal popup?

The problem was that the GridView in the model-popup was inside an UpdatePanel (thanks to nunespascal for pointing this out).
To solve this, I added this code:
Protected Sub AddressGridView_RowDataBound(...) Handles AddressGridView.RowDataBound
Dim AddressNameLinkButton As LinkButton = e.Row.FindControl("AddressNameLinkButton")
ScriptManager.GetCurrent(Page).RegisterPostBackControl(AddressNameLinkButton)
End Sub
Now when I press one of the LinkButtons in the GridView, the modal-popup closes and the address form is filled

Related

Transfer radio button from webform to another form using VB.net and ASP.net

I wrote the below code which transfer the radio button value and check box value to another HTML form but i did not find solution to transfer the radio button or check box at all to another form not only the selected value.
I want the radio button to be transfered to another form as below not only the value.
enter image description here
Dim Gender As String = RadioButton1.SelectedValue
Response.Redirect("PrintPreview.aspx?"&Gender=" + Gender)
Label1.Text = Request.QueryString("Gender")
The code only returned the radio button value
Please advise
Ok, the FIRST thing, and MOST important thing to realize here is that when you execute a response.Redirect ?
It STOPS code running in the current page.
No code AFTER the Response.Redirect will run
All variables, and code and ALL values for the current page are destroyed!
So, you can't write (normally) code to run after the response.Redirect.
So, say we have this markup:
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
Font-Size="Larger" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="Button1" runat="server" Text="Done" />
And our page now looks like this:
Now, we want to jump to page 2.
So our code can say look like this in our Test1 page.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Gender As String = RadioButtonList1.SelectedItem.Text
Response.Redirect("Test2.aspx?&Gender=" & Gender)
' code AFTER above line WILL NOT run!!!!
End Sub
So, we can't have code AFTER the response.Redirect.
Again, read this 5 times:
when the response.Redirect is used, then the current page code STOPS,
and ALL values, and even your code variables are DESTROYED!!! This is not much
different then when you get to the end of a subroutine - when you exit, then all
values and things in that subroutine are "gone", and "do not exist".
The same goes for your web page - using Response.Redirect means STOP code, transfer to another page.
So, now above will jump to page Test2, we want to take that value we passed, and do this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' save a label on this page to the passed choice
Label1.Text = Request.QueryString("Gender")
End If
End Sub
Also, note close your syntax for the string you were passing in Response.Redirect was also incorrect.

asp:textbox calling asp:button handler on text change

I have the most bizarre problem happening ... I'm controlling a menu edit page and I have a series of controls, an asp:textbox with the numeric ID of the menu being worked on, flanked by - and + buttons to decrement and increment the ID. The user can type a new index in the textbox, or use the buttons. Easy peasy, right?
Here's the ASPX markup:
<asp:Button id="menuDown" runat="server" CausesValidation="false" OnClick="DownClick" Text="-" />
Menu # <asp:textbox runat="server" MaxLength="5" Columns="3" id="tbMenuId" />
<asp:Button id="menuUp" runat="server" CausesValidation="false" OnClick="UpClick" Text="+" />
There's a CompareValidator further down the page ensuring a non-negative, numeric entry.
Over in my code behind, my Page_Load pulls the value out of the textbox without problems:
Try
menuId = Integer.Parse(tbMenuId.Text)
Catch
menuId = 0
tbMenuId.Text = "0"
End Try
This works great (with ONE caveat) — every time I change the menu ID via the textbox, after the Page_Load method pulls the new value out of the text box perfectly, the DownClick handler gets called, decrementing the desired ID.
So far, the ONLY way I've managed to have the requested menu appear is to eliminate the two asp:Button controls. Eliminating only the menuDown button causes the UpClick handler to get called.
I had an OnTextChanged="..." attribute on the asp:Textbox control, but that handler NEVER got called and didn't seem necessary anyway, so I pulled it.
I cannot help but think the two behaviors must be connected.
Anyone understand WHY this is happening?
This qualifies as hacky work-around, rather than a fix, but it does let me proceed forward until this behavior can be explained.
The OnTextChanged event is firing the way it's supposed to now (not sure what I did wrong the first time) so, I used it to implement the hackiest of hacks ...
Protected Property hackyFlag As Boolean ' Suppress button click (true) after text change
' ... value in session variable
Protected Sub tbMenuId_TextChanged(sender As Object, e As EventArgs)
Integer.TryParse(tbMenuId.Text, menuId)
hackyFlag = True
End Sub
Protected Sub DownClick(sender As Object, e As EventArgs)
If hackyFlag Then : hackyFlag = False : Return : End If
If menuId > 0 Then menuId -= 1
tbMenuId.Text = menuId
End Sub
Gross, hacky, and disgusting ... but it works. :-) The button handler is still being called incorrectly, but now it's smart enough to do what I want instead of what the framework is telling it to do.
Still open to better answers and/or explanations as to why this is happening on this page and hasn't happened on any other. ( knock on wood )

Creating a Custom Button in a ListView in ASP.NET

I have a Results.aspx page that displays the resulting records queried using a SqlDataSource object via a ListView. I want to add a "View" button that will appear next to each record, and when clicked will take me to a separate page that will display details about that record. How do I accomplish this?
Edit
I have tried what you said, citronas and here's what I've come up with:
<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="ViewButtonClick" CommandArgument='<%# Eval("ServiceId") %>'>View</asp:LinkButton>
</td>
And here is the method that I want to be called:
protected void ViewButtonClick(object sender, CommandEventArgs e)
{
var serviceId = Convert.ToInt32(e.CommandArgument);
ServiceToView = DataAccessLayer.Service.Select(new Service { ServiceId = serviceId });
Server.Transfer("~/ViewService.aspx");
}
Unfortunately nothing actually happens...am I missing something?
Edit -- Fixed
I was missing something! I had CommandName equal to my method name instead of OnCommand. I took out CommandName, kept the argument bit and replaced CommandName with OnCommand. Everything works now, but what would I ever need CommandName for?
You can add a LinkButton into the ItemTemplate of the ListView.
Bind the value that identifies each record to the CommandArgument of the LinkButton.
Subscribe to the Command-Event of the LinkButton. There you have access to CommandEventArgs.CommandArgument
What you wound up doing worked Storm. I decided to go with Citronas' suggestion and share my answer.
FIRST:
On the aspx I added a LinkButton to my ItemTemplate with my own CommandName and CommandArgument. I passed my item's ID as a CommandArgument so I could later use it inside my sub.
<asp:LinkButton ID="lnkBtnAnswers" runat="server" CommandName="Answers"
CommandArgument='<%# Eval("ID")%>'>Answers</asp:LinkButton>
SECOND:
On the codebehind I created a sub that would be called whenever the user conducted an action. As Citronas mentioned normally you use "Select", "Add", "Edit", or "Delete" here. I decided to create "answers".
Note: Handles MyControl.ItemCommand is very important here as it is what subscribes you to the command event.
Protected Sub lvQuestions_Command(sender As Object, e As CommandEventArgs) Handles lvQuestions.ItemCommand
If e.CommandName.ToLower() = "answers" Then
hfSelectedQuestionID.Value = e.CommandArgument
End If
End Sub
Done! Now since every command goes through the new sub, it is important to check for the right commandName so you can conduct the appropriate action. Don't forget to use the CommandArgument to your advantage.

asp.net usercontrol SetFocusOnError

I have a aspx page that several of the same usercontrols on the page. The usercontrol houses a textbox that has a Required field validator on it. The validator works but the setonfocus="true" does not seem to be working, further more, the button the aspx page when the validator shows the error message, the button still fires the code behind.
Here is what the aspx page looks like as far as the user control and the the button.
ucTB:ucTextBox ID="ucTextR" runat="server" ValidationGroup="txtRequired" Required="_true"
asp:Button ID="btnSave" runat="server" Text="Click" ValidationGroup="txtRequired"
and the usercontrol validator
asp:RequiredFieldValidator ID="rfTextBox" runat="server" ControlToValidate="txtTextBox"
SetFocusOnError="true" ErrorMessage="Required Field" EnableClientScript="false"
the user control has been wired to grab the validator from the aspx page and use it in the usercontrol... something like this
Public Property ValidationGroup() As String
Get
Return CType(ViewState("ValidationGroup"), String)
End Get
Set(ByVal Value As String)
ViewState("ValidationGroup") = Value
End Set
End Property
Protected Sub AssignValidation()
For Each control As Control In Me.Controls
Dim [property] As PropertyInfo = control.[GetType]().GetProperty("ValidationGroup")
If [property] Is Nothing Then
Continue For
End If
[property].SetValue(control, ValidationGroup, Nothing)
Next
End Sub
and i load the AssignValidation on page_load
anyway.. hope this is the info you need to point me in the right direction.
What i'm looking to do is if the required field validator to put the focus on the usercontrol if there is nothing in the usercontrol text box and also for the button on the aspx page not to fire.. like i think it behaves if you use a validator on a aspx page with no usercontrol
thanks
shannon
You can't set the user control to visible because it's not a visible container. You can set the focus yourself programmatically. See this:
http://forums.digitalpoint.com/showthread.php?t=282224
Or, you can programmably set the validator to the ID of the textbox within the user control; expose a textboxID property in the user control code-behind which returns the textbox's ID, and have your page assign the validator's controltovalidateID to this.

AutoCompleteExtender control in a repeater

I have an AutoCompleteExtender AjaxControlToolkit control inside a repeater and need to get a name and a value from the web service. The auto complete field needs to store the name and there is a hidden field that needs to store the value. When trying to do this outside of a repeater I normally have the event OnClientItemSelected call a javascript function similiar to
function GetItemId(source, eventArgs)
{
document.getElementById('<%= ddItemId.ClientID %>').value = eventArgs.get_value();
}
However since the value needs to be stored in a control in a repeater I need some other way for the javascript function to "get at" the component to store the value.
I've got some JavaScript that might help you. My ASP.Net AutoComplete extender is not in a repeater, but I've modified that code to detect the ID of the TextBox you are going to write the erturned ID to, it should work (but I haven't tested it all the way through to post back).
Use the value from 'source' parameter in the client side ItemSelected method. That is the ID of the calling AutoComplete extender. Just make sure that you assign an ID the hidden TextBox in the Repeater Item that is similar to the ID of the extender.
Something like this:
<asp:Repeater ID="RepeaterCompareItems" runat="server">
<ItemTemplate>
<ajaxToolkit:AutoCompleteExtender runat="server"
ID="ACE_Item"
TargetControlID="ACE_Item_Input"
...other properties...
OnClientItemSelected="ACEUpdate_RepeaterItems" />
<asp:TextBox ID="ACE_Item_Input" runat="server" />
<asp:TextBox ID="ACE_Item_IDValue" runat="server" style="display: none;" />
</ItemTemplate>
</asp:Repeater>
Then the JS method would look like this:
function ACEUpdate_CustomerEmail(source, eventArgs) {
UpdateTextBox = document.getElementById(source.get_id() + '_IDValue');
//alert('debug = ' + UserIDTextBox);
UpdateTextBox.value = eventArgs.get_value();
//alert('customer id = ' + UpdateTextBox.value);
}
There are extra alert method calls that you can uncomment for testing and remove for production. In a simple and incomplete test page, I got IDs that looked like this: RepeaterCompareItems_ctl06_ACE_Item_IDValue (for the text box to store the value) and RepeaterCompareItems_ctl07_ACE_Item (for the AC Extender) - yours may be a little different, but it looks practical.
Good Luck.
If I understand the problem correctly, you should be able to do what you normally do, but instead of embeding the ClientId, use the 'source' argument. That should allow you to get access to the control you want to update.
Since you are using a Repeater I suggest wiring the OnItemDataBound function...
<asp:Repeater id="rptResults" OnItemDataBound="FormatResults" runat="server">
<ItemTemplate>
<asp:PlaceHolder id="phResults" runat="server" />
</ItemTemplate>
</asp:Repeater>
Then in the code behind use something like
`Private Sub FormatResults(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
Dim dr As DataRow = CType(CType(e.Item.DataItem, DataRowView).Row, DataRow) 'gives you access to all the data being bound to the row ex. dr("ID").ToString
Dim ph As PlaceHolder = CType(e.Item.FindControl("phResults"), PlaceHolder)
' programmatically create AutoCompleteExtender && set properties
' programmatically create button that fires desired JavaScript
' use "ph.Controls.Add(ctrl) to add controls to PlaceHolder
End Sub`
Voila

Resources