How do you determine the client side NAME of a server control? - asp.net

Let's say I have a DropDownList server control, called "CategoriesDDL" and the ClientID proeprty determines its client side id, which is its ID prefixed with the id's NamingContainer's ids. In this case the client side ID is CP1_CategoriesDDL. But what is the rule regarding the client side name, in this case "ct100$CP1_CategoriesDDL"?

Any chance you're simply after the Control.UniqueId property?
Server side, this will return the client side "name" attribute value of the control.

Are you using ASP.NET 4? If thats the case, the default for the ClientIDMode property on server controls is "Predictable". If you change that to Auto, you will get same client id and client name except "_" and "$". So on the Server Side, you can use client id, replace "_" with "$" to get the client name.
Also look out for the ClientIDMode="Static", this will simplify it greatly.
If you are not using ASP.NET 4, seems there is some different reason for your issue.

Related

Single Page Applications using ASP Web Forms

I need to implement single page applications using ASP Web Forms. I faced with a navigation problem. I need to use a navigation pattern like this:
http:// web site url / ... / page.aspx? {query string} # {ListId} / {ItemId}
When a user request a data from the server, the request on the server doesn't contain hash # (because this is a client-side feature). And it looks like this:
http:// web site url / ... / page.aspx? {query string}
So, actually I need two requests:
to get a page without hash and load javascript;
to handle hash data using javascript and async call required data from the server.
Is it possible to implement this logic with only one request?
Are there any best practices?
You can append ListId/ItemId to query string before sending request and read it regularly on a server.
var url = 'http://example.com?param1=10&param2=20#1000';
var beforeHash = url.split('#')[0];
var itemId= url.split('#')[1];
var processedUrl = beforeHash + '&itemId=' + itemId;
If your request is not already fired from JavaScript, you will have to hook into link's click event...
Or maybe you can get rid of # entirely and scroll content via JavaScript (my guess is that you use # because of local anchors to jump to different places in document)?
BTW There is window.location.hash property.
Update:
Based on your comment the flow is like this:
User types URL with #ItemId
Server returns the page
JavaScript reads #ItemId from window.location, puts it into QueryString and makes a request
Server returns the page based on modified QueryString
In this situation the two-requests pattern seems to be the only viable option. By design server does not get #Item part (called fragment). So there is no way to guess ItemId upon initial request. If after second (ajax) request, you refresh #ItemId dependant parts of the page through JavaScirpt, user experience will not be hindered much.

different versions of output caching based on selected values of several dropdownlists

So this is about output caching, and I am trying to use <%# OutputCache VaryByParam="">, how to realize this?
VaryByParam refers to the query string parameters in the request url. So you would need to submit the dropdownlist values as part of the url query string.
You would probably need to use javascript to alter the request url since you would not know the selected values server side.

Find non server control attribute value in postback?

I can get the value of non server control with Request.Form["fieldID"];
how i can get the attributes of these control?
some think like these :
Request.Form["fieldID"].Attribute["the_Attribute"].value;
or any other way
You're able to get the fieldId because its value is included in the POST, its attributes however will not be.
I'd recommend using something like firebug or fiddler to see the info that's coming in on the request, or even using the locals or immediate window to explore the Request.Form object to get a feel for what you can/can't do in this kind of situation.
There are no attributes. The only thing sent in the form data to the server is the value from the form field.

How to get data from a control into another ASP.net page?

I'm creating a time sheet for work to learn more about asp and making database connections I am also using this time to prepare for my next C# and database design class which start on Wednesday. I'd like to know how I can get data from default.aspx and display it in timesheetdisplay.aspx, and I would also like to know how I can make it so the person doesn't have to enter the full id "100000111" as it appears in the database just the last 3.
<asp:TextBox id="xBadgeTextBox" runat="server" width="100px"></asp:TextBox>
As far as passing data between pages you can pass it via QueryString, Session variables, or by persisting it to some sort of data store such as a Database. In the situation above I would look at passing via Querystring parameter. Be sure that if you do do this that you validate the data on the new page to ensure its safety and validity before using it (think SQL Injection Attack).
How to: Pass Values Between ASP.NET Web Pages
As far as your second question goes I would say that this could be handled on the server side if you are sure that the last 3 digits will always be unique. Or were you looking to prompt the user entering data similar to Google? If so look at the AutoComplete Extender in the AJAX Control Toolkit or look at doing something similar in JQuery.
If you're redirecting from page to page, consider using the Server.Transfer("timesheetdisplay.aspx", true) method when navigating away from your default.aspx page. Note the second parameter, true, which will persist all ViewState and QueryString data across from page to page.
I would generate a unique key, store the value you are transfering in the users session, redirect the user and include the key in the query string, grab the key, and then get the value. Something like this:
//---On Default---
var value = "can be a single string or even a complext object...";
var keyName = Guid.NewGuid().ToString();
HttpContext.Current.Session[keyName] = value;
HttpContext.Current.Response.Redirect("timesheetdisplay.aspx?SID=" + keyName);
//---On TimeSheet---
var getKeyName = HttpContext.Current.Request.QueryString["sid"].ToString();
var myValue = HttpContext.Current.Session[keyName];
To get the id from a partial ID I would do it just like Muhammad Akhtar said:
select * From yourtable where id like '%111'

How to block IP address or IP classes in ASP.NET

I need to block one IP address or class in asp.net
Can anyone help me with the code? And how to implement?
Thanks
You can get the IP address of the client using the HttpRequest.UserHostAddress property (an instance can be accessed using this.Request from any page or using static property HttpContext.Current).
As far as I know, there is no standard method that would compare the IP address with a specified range, so you'll need to implement this bit yourself.
You'll probably want to check this for every request, which can be done either in the OnInit method of every page (that you want to block) or in the BeginRequest event of the application (typically in Global.asax).
If you detect a blocked address, you can output an empty (placeholder) page using Server.Transfer method (Response.End would be another alternative, but that simply cuts the page - returning an empty page, while Server.Transfer allows you to output some message to the client).
If what you mean by "block" is "don't let them harass my server", this is not an asp.net issue, you need a firewall (software or hardware).
If what you mean by "block" is "don't show my pages":
' pseudocode, I haven't checked the exact syntax
Sub Page_Load()
If HttpRequest.UserHostAddress = "123.123.123.1" then
Response.Redirect "404.htm" ' send them elsewhere
end if
End Sub
you mention you are not familiarized with the ASP.NET, so, maybe this excelent article from Rick can help you as it as a full article on how to block IP's and even have an admin area to manage them...
http://www.west-wind.com/WebLog/posts/59731.aspx

Resources