Showing posts with label access. Show all posts
Showing posts with label access. Show all posts

Monday, March 26, 2012

How do I include cookies with a clientside webrequest?

I am sending a webrequest to an aspx page that expects a cookie to exist.

If I access the page directly, it can access the cookie fine. If I access the page over the webrequest, the cookie count is 0 (according to HttpContext.Current.Response.Cookies.Count.ToString()).

Now, on the serverside examples, it seems people have to set the CookieColelction property of the WebRequest obejct. Unfortuantly, according to the documentation -http://ajax.asp.net/docs/ClientReference/Sys.Net/WebRequestClass/default.aspx there is no such property to set in the ajax clientside version of webrequest.

How do I send cookies over a clientside webrequest?

HTTP Cookies are a state management implementation of the HTTP protocol and many Web pages require them. If you're using remote HTTP functionality to drive a Web site (following URLs and the like) you will in many case have to be able to support cookies.
Cookies work by storing tokens on the client side, so the client side is really responsible for managing any cookie created. Normally a browser manages all of this for you, but here there's no browser to help out in an application front end and we're responsible for tracking this state ourselves. This means when the server assigns a cookie for one request, the client must hang on to it and send it back to the server on the next request where it applies (based on the Web site and virtual directory).HttpWebRequest andHttpWebResponse provide the container to hold cookies both for the sending and receiving ends but it doesn't automatically persist them so that becomes your responsibility.
Because the Cookie collections are nicely abstracted in these objects it's fairly easy to save and restore them. The key to make this work is to have a persistent object reference to the cookie collection and then reuse the same cookie store each time.
To do this let's assume you are running the request on a form (or some other class – this in the example below). You'd create a property called Cookies:
CookieCollection Cookies;
On the Request end of the connection before the request is sent to the server you can then check whether there's a previously saved set of cookies and if so use them:
Request.CookieContainer = new CookieContainer();
if (this.Cookies != null && this.Cookies.Count > 0)
Request.CookieContainer.Add(this.Cookies);

So, if you previously had retrieved cookies, they were stored in the Cookies property and then added back into the Request'sCookieContainer property. CookieContainer is a collection of cookie collections – it's meant to be able to store cookies for multiple sites. Here I only deal with tracking a single set of cookies for a single set of requests.
On the receiving end once the request headers have been retrieved after the call to GetWebResponse(), you then use code like the following:
// *** Save the cookies on the persistent object
if (Response.Cookies.Count > 0)
this.Cookies = Response.Cookies;

This saves the cookies collection until the next request when it is then reassigned to the Request which sends it to the server. Note, that this is a very simplistic cookie management approach that will work only if a single or a single set of cookies is set on a given Web site. If multiple cookies are set in multiple different places of the site you will actually have to retrieve the individual cookies and individually store them into the Cookie collection. Here's some code that demonstrates:
if (loWebResponse.Cookies.Count > 0)
if (this.Cookies == null)
{
this.Cookies = loWebResponse.Cookies;
}
else
{
// If we already have cookies update list
foreach (Cookie oRespCookie in loWebResponse.Cookies)
{
bool bMatch = false;
foreach(Cookie oReqCookie in this.oCookies) {
if (oReqCookie.Name == oRespCookie.Name) {
oReqCookie.Value = oRespCookie.Name;
bMatch = true;
break;
}
}
if (!bMatch)
this.Cookies.Add(oRespCookie);
}
}
}
You can try to take a look at this link for details - http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm
Wish the above can give you some ideas.

Saturday, March 24, 2012

how do I access data on client side with ajax

hello. I have an mdb that I want to use to popluate some textboxes with out postback or waiting for IE to reload.

I am using easylist box that gives me the record ID but I want to use that ID to pull data from the MDB with out reloading the IE.

can someone link me in the right direction?

You should issue XmlHttpRequest on some Javascript event (say, button click or focus removed or whatever), which will pass your ID to the server side and will get the response, which you should dynamically insert into the TextBox you want (javascript, client side again).

This is th schema in general.

You can find a lot of free books about AJAX on the net, ex.: http://paperolli.com/default.aspx?keyword=ajax


thanks!!!

How do i access controls inside the update progress or update panel.

How do i access controls under the update progress or update panel.

I have 1 Update progress then i want to access the label inside the update progess. I want to change the text property of the label when my edit image button allter it. thanks

You will need to find the control recursively. However, the FindControl method will not find controls recursviely. Use this cool method FindControlRecursive(Control root, string id) by Jeff Atwood. The reason you cant access them otherwise is because they are child controls. Visit Jeff Atwoods article athttp://www.codinghorror.com/blog/archives/000307.html I use that to find controls. Once you find the control you will need to cast it to the type of control it is. Example:

TextBox tb = (TextBox)FindControlRecursive(Page,"txtNameOfControl");

tb.Text ="Hello there";

The above is an example as if you were finding a textbox.

Let me know if you need more help!

John


hello.

well, yes and no. for updatepanels, you don't need to use findcontrol since the controls are injected on the page as fields (which means you can simply use its id to get a reference to them)

regarding the updateprogress, things get a little messy. here's an example that shows how to change it during a full postback:

<%@. Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
void E(Object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
}

private Boolean update = false;

void J(Object sender, EventArgs e)
{
update = true;
}

protected override void Render(HtmlTextWriter writer)
{
if (update)
{
Label lbl = (Label) progress.FindControl("lbl");
lbl.Text = DateTime.Now.ToString();
}
base.Render(writer);
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" ID="panel">
<ContentTemplate>
<asp:Button runat="server" id="bt" text="partial postback" OnClick="E"/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button runat="server" text="full postback" OnClick="J" />
<asp:UpdateProgress runat="server" ID="progress" AssociatedUpdatePanelID="panel">
<ProgressTemplate>
<asp:Label runat="server" id="lbl" />
</ProgressTemplate>
</asp:UpdateProgress>
</form>
</body>
</html>


Acutally I am trying to recall my orginal issue and why I used FindControlRecurvively and I beleive it was because I had a Drop Down List in a custom server control which was in an tab panel. The version of Ajax.net at the time had an issue with doing that and it throwing a null reference. This was some months ago so I don't recall all the logistics:

If you use FindControl to access a control inside a TabPanel, it throws a NullReferenceException.This behavior appears in the 10606 release10301. If you compile the same application with the March release (10301) it works well.
http://www.codeplex.com/AtlasControlToolkit/WorkItem/View.aspx?WorkItemId=11168


hello again.

ok, fair enough. but regarding the controls you've asked about (updatepanel and udpateprogress) my previous observations are correct.


Thanks for the reply, it works I've just create a new instance of label then assign the findcontrol to it.

dim lbl as label

lbl = updatepanel1.findcontrol("label1")

lbl.text = "hello"

thanks


hello again.

hum...why are you doing that? I mean, the controls declared inside an updatepanel are also inserted on the page as fields so you can only use its id to get them (this is not true for controls that are inside an updateprogress control)


Thanks for concern, I try it but it does not work, for example i have label1 inside the updatepanel1. So how do i access the label1 and change the text property?

Can you teach me how... Thanks again...


hello.

well, you just use its id:

label1.Text = "something";

do keep in mind that this code will only work if you're using it frmo the same page or user control that has the updatepanel which has the label inside...


Dim pwdAs ChangePassword

pwd = updatePanel1.FindControl("ChangePassword1")

pwd.Visible =True

This worked for me too, thanks guys.

Sasanka Pinidiya

How do I access controls in a TabContainer for FormView Select Parameters

I have a page with a gridview which is inside a tab panel.

Depending upon the selection in the GridView I need to set a Select Parameter for a FormView on another area of the page.

I simply get the error "Could not find control 'GridView2' in ControlParameter 'PoemID'"

Any suggestions ?

i got the same problem trying to access a label inside a tabcontainer...

someone pease help us!


I have had some success with resolving a similar problem by adding the name of the container to to the ControlId parameter as follows: ContainerName$Gridview2

I have an UpdatePanel with a textbox inside, further down the page I have a tab panel that needs to reference the value entered into the textbox and was able to get it to work by having the following syntax:<asp:ControlParameterControlID="UpdatePanel1$TextBox1" ...... what I can't work out is how to reference the same textbox from the tabpanel if it was placed within another tabpanel above or a different container such as an accordion panel. let me know if this gives you any clues...


Found any solution yet? I'm also facing the same problem. Thank you for sharing.

Men, thanks to you I could fix my application...

You just have to do the same with all the controls of the panel, but remember to use the tab container and the tab panel, OK... Here my example... Thnaks

--

SelectMethod="GetEmpleado_MByNumero"TypeName="RecursosHumanoBLL"UpdateMethod="UpdateEmpleadosMByNumeroEmpleado"OldValuesParameterFormatString="{0}">

<SelectParameters>

<asp:ControlParameterControlID="udpConsultaEmpleado$tcEmpleados$tpFiltro$gvEmpleados"DefaultValue="1"Name="Numero"

PropertyName="SelectedValue"Type="Int32"/>

</SelectParameters>

--

I hope it work for you too

How do I acces profiles from a web service called from Atlas?

I have a web service that is called by a page that employs Atlas. I was wondering if there is a way to access the profile of the user from the webservice?

I'm trying to use HttpContext.Current.Profile.GetPropertyValue() but it's returning empty data. Is there anything that I have to set up?

Hi Zephyr,

Can you check to see whether the User.Identity.Name property is available in your web-service? This will indicate whether the user is logged in when the web-service is called. My guess is that they aren't - which is why the Profiloe is coming back null.

Thanks,

Scott

How can we access a control outside the update panel with an control inside the update pan

Take a very simple example, i have a button inside the update panel along with other controls and i have a textbox on the page.What i want is that when i click the button inside the update panel,it should update the textbox which is present outside the update panel.How can i do this?

To my knowledge you cannot update any control outside of the update panel. If you want to update the control move it inside the update panel.

Do you want to modify the text box with server-side code? If so, put it in its own UpdatePanel. Set the UpdateMode on the panel to Conditional. Then you can either add the button in the other UpdatePanel as an AsyncPostBackTrigger to the new UpdatePanel or invoke the Update method on the new UpdatePanel in your button's Click event handler.

If you want to modify the text box on the client, you're going to have to write JavaScript.


Can you please tell me what javascript should i write for the sition i mentioned in the above example.

I am not getting the idea using javascript for a control inside the update panel.


Are you sure you need to write JavaScript? I gave you the steps to modify the text box using purely server-side code by using a second UpdatePanel control. I highly recommend you read theUpdatePanel tutorials and then re-read what I suggested. This path doesn't require JavaScript, but does require a round-trip to the server which is where your code will be executing.

If you do need to use JavaScript, you're going to need to handle the button's click event, modify the other control, and then prevent the default action for clicking the button from occurring (if necessary). This would all be done on the client so wouldn't require a round-trip to the server, but involves learning JavaScript and the DOM API (which you should want to learn as a web developer, anyways).


i have read it but still i feel i need javascript.Can u help me?