Hyperlink inside dynamic userControl - asp.net

I have a userControl being dynamically added to my form, anywhere from 1 to 20 times.
The control contains hyperlink field (visible) and a reference field (invisible). The problem is that the hyperlink is not passing the value of the reference field as linked page parameter.
The userControl coding looks like this:
<asp:Hyperlink ID="childLink" Enabled="true" DataNavigateUrlFields="aSeq"
DataNavigateUrlFormatString="~/Header.aspx?aSeq={0}" NavigateUrl="~/Header.aspx"
runat="server><%# DataBinder.Eval(Container, "DataItem.cName") %> </asp:HyperLink>
So, the hyperlink should open the Header page, passing the value of the "aSeq" field which looks like this:
<asp:HyperLink ID="aSeq" Enabled="true" DataNavigateUrlFields="aSeq" DataNavigateUrlFormatString="~/Header.aspx?aSeq={0}" NavigateUrl="~/Header.aspx"
runat="server"><%# DataBinder.Eval(Container,"DataItem.genePK") %></asp:HyperLink>
It does go to the page load for Header.aspx page, but the Request.QueryString["aSeq"] is always NULL. On the Page_Load I have a short block of code:
string aRequest = Request.QueryString["aSeq"];
if (aRequest==null)
{
PopulateHeader("GKEAAHDI");
}
else
{
PopulateHeader(aRequest);
}
I can only surmise it has not gotten the value of the field tagged "aSeq". I used a hyperlink because a textbox will not allow a code block for the databinder.
What have I done!?

According to MSDN " The value of the field specified by the DataTextField property is passed as a query string to a Web page specified in the format string." So you would need to add DataTextField="aSeq" in your asp:hyperlink declaration.
Not tested, just researched.

Related

Reference to querystring parameters in .aspx markup code

I'm trying to reference a query string parameter inside an HyperLink element :
<asp:HyperLink runat="server" NavigateUrl='<%# Eval(Request["id"], "~/PATH/Page.aspx?id={0}") %>' Text="reload" />
I tought it was simple... but it's not: NavigateUrl property probably fails evaluation an the resulting url is empty.
Of course Request["id"] is valid at the time the page is evaluated.
I tried also using string.format() instead Eval() with the same result.
This means <%# ... %> data-binding expression
The data-binding expression creates binding between a server control property and a data source when the control's DataBind method of this server control is called on the page.
But you are only formatting a string to set for your NavigateUrl
Inline code block is not allowed to any server tag controls
those that have runat="server"
You can only set it via code either eg: inside the Page_Load
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.NavigateUrl = string.Format("~/PATH/Page.aspx?id={0}", Request["id"]);
}
Your second post work's because it's a pure html client control and not a server control.
Source: Inline Expressions
I didn't fully understand what the problem was but the following code works:
<a href="~/PATH/Page.aspx?id=<%= Request.QueryString["id"].ToString() %>" >reload</a>
I tried several times including one using <a></a> tag with runat="server" property (not working).

How to use Request.Form to retrieve value when using masterpages

I have setup a hiddenfield control and a linkbutton on an aspx Masterpages content page.
<asp:HiddenField ID="HiddenField1" runat="server" value='<%# Eval("ID") %>'/>
<asp:LinkButton ID="LinkButton1" runat="server" postbackurl="orderhistorydetail.aspx">View</asp:LinkButton>
When trying to retrieve the value on the postback page using this code...
string oid = Request.Form[HiddenField1];
I am getting the error...
The name 'HiddenField1' does not exist in the current context.
Is this because of materpages? How can I fix this?
EDITED...
I viewed the source html that was generated for the page that has the hiddenfield control on it and this was the output...
<input type="hidden" name="ctl00$MainContentPlaceHolder1$ListView1$ctrl0$ctl00$HiddenField1" id="MainContentPlaceHolder1_ListView1_ctrl0_HiddenField1_0" value="12386026" />
Now, as for the code you originally gave me, I updated it to this...
HiddenField hf = Page.PreviousPage.Master.FindControl("MainContentPlaceHolder1").FindControl("ListView1").FindControl("HiddenField1") as HiddenField;
Still no luck. Am I getting close?
The page you are posting to has no knowledge of the Controls that are present on the previous page. You need to use FindControl for that.
HiddenField hf = Page.PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("HiddenField1") as HiddenField;
string oid = hf.Value;
Or if you really want to use Request.Form you need to use the UniqueID.
string oid = Request.Form[HiddenField1.UniqueID];
First you must find the PlaceHolder of the Page that is using a Master Page and then the correct Control within the PlaceHolder.
If you want to check what all the Posted values are, use this:
foreach (string s in Request.Form.Keys)
{
Response.Write(s.ToString() + ": " + Request.Form[s] + "<br>");
}

How can I set a label from the code behind of a page?

Upon page load I want to set the content of a label on a page. I'm moving from MVC and is used to set this in the View return function of that controller. But how can I do this with ASP.NET Forms, if I wanted to fetch the weather at the users location I would want to write "Hello user, it is sunny outside" in the code behind and send it to the page - but how?
On the aspx, just add a label with id and runat server like this:
<asp:Label id="myLabel" runat="server" />
And in the code behind you can set the text like this:
myLabel.Text = "SomeText";
just add a asp:Label control on the page, give it a ID like ID="myLabel" and runat="Server" and then in your code behind set the label text as myLabel.Text = "Hello user, it is sunny outside";

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