Showing posts with label void. Show all posts
Showing posts with label void. Show all posts

Monday, March 26, 2012

Pass value to Web User Control

Say I have a user control like this one...
private Sections s = new Sections();

private void Page_Load(object sender, System.EventArgs e)
{
}

public Sections Sections
{
get { return s; }
set { s = value; }
}

public void f()
{
if (s.Query.Load())
while (s.MoveNext())
Response.Write(string.Format("<tr><td width='5'><img src='images/arrow.gif'></td><td><a href='showsections.aspx?s={0}'>{1}</a></td></tr>", s.SectionId, s.Title));
}
And I dragged the user control to index.aspx. Now, on page load of index.aspx, I want to pass some value to the user control's Section property. How do I do this? Or is this possible?

Thanks.If you have the User Control on the Page then you can simply use myUserControl.Sections = someValue; This is same as you would do for any other control on your page.
The thing is, even if i dragged the UserControl on my index.aspx, I can't seem to find SectionsControl (my web user control). At design mode though, I can see SectionsControl1 on index.aspx.

Am I missing something here?
Yes that is pretty much the behaviour you expect for any other control you add to a page. When you add a textbox control first time, the name of textbox will be textBox1 and second control will the name textBox2. In the same manner when you add a usercontrol, it will also follow the same behaviour. So in your code you should write this SectionsControl1.Sections = someValue;
It's a bug in VS 2003. You add a control to your page, but it fails to show up in your codebehind.

Go into the codebehind designer region, and declare your control (of the same name as on the page) with events.
Or, when you add your control to your page by dragging, save your page, THEN go into codebehind. It should show up there.
I missed this post last days. Sorry for the late reply. A bug that is, so decided to declare it myself. Thanks guys.

Wednesday, March 21, 2012

passing a Guid to a button class

Hi i have a linkbutton created with C# that needs to call a class and pass a guid through to that class i have tried to use

public void buttonClass(string guid,object sender,Eventargs e)
but it does work i get an error so how do i pass a guid to this class form my button ( code below)

1//button class2public void prodAdd(object sender, EventArgs e)3 {4 checkoutFunctions cartAdd =new checkoutFunctions();5string userGuid = cartAdd.userFind(HttpContext.Current.User.Identity.Name);6 cartAdd.addItem(prodGuid, userGuid);7 Response.Redirect("cart.aspx");8 }910// BUtton generator11 mediaSelect.Controls.Add(new LiteralControl("<a href='#' onClick=\"flashshowHide('" + mediaId + "')\">" + medRead["media_name"].ToString().Replace("#@dotnet.itags.org.","'") +"<span class='prevBut'>(preview)</span></a>"));12 Button buyProd =new Button();13 buyProd.Text = ("Download for £" + medRead["media_price"].ToString());14 buyProd.Click +=new EventHandler(prodAdd);15 mediaSelect.Controls.Add(buyProd);16 mediaSelect.Controls.Add(new LiteralControl("<br/>"));
Thanks
Dan 

I think you must be missing something here conceptually. The prodAdd isnt a button class, it's an event handler that you're wiring up to handle the click event of your dynamically created button. That set of code will run only when the button is clicked. If you need to pass a GUID to it, pass it from wherever it was clicked at, not when the button is created.


yes ok but how do i set it to send the guid to the class when it is clicked


It's not a class - it's an event handler. You are going to have issues till you can get your terminology correct.

Try

buyProd.commandArgument = yourGUID

then in your event handler you can get the guid by using

e.commandArgument