textbox controls are display the main page in asp.net - asp.net

I have designed the basic controls application in asp.net.---> Using the 3 controls are display the choosing controls.---->controls are added on the dropdownbox.-----> Values are enter the textbox.-----> Click to the button.
I have click the button is to show on the choosing controls are displayed it.
i had attached the snapshot.
enter image description here

If I get it correctly, you want to display appropriate asp.net control in a rectangle as displayed in the image?
Basically, I would go about it like this:
Add placeholder on main page. This placeholder will be used to store conrols.
Handle button click event in which you create desired controls and add them to placeholder
Main page addon:
<asp:Placeholder runat="server" ID="phControls"></asp:Placeholder>
and where button is, you need to add OnClick event handler
<asp:Button runat="server" ID="btnAddControl" ... OnClick="btnAddControl_Click"></asp:Button>
Main page code-behind:
...
protected void btnAddControl_Click(object sender, EventArgs e) {
if (myDropDown.SelectedValue == "TextBox") {
var tb = new TextBox();
tb.Text = tbEntry.Text;
phControls.Controls.Add(tb);
}
...
}
Something like that. Would prefer more information and some of your code though.

Related

Traversing values from one page to the other page

I have 2 aspx pages.. (view.aspx,edit.aspx).
under view.aspx I have grid which displays the following fields.
class,photo,photocaption,Edit which consists of class name, image of the class and a caption for the photo and a link button for edit which traverses to edit.aspx...
under edit.aspx, I have a table consisting of
Class(a drop down box),
Photo(text box with BROWSE button),
photocaption(Textbox).
when i click on the edit in view.aspx, I must get the entered values in the view.aspx grid by default in edit.aspx table..
Please help me to finish my Task...
Assuming your view.aspx page has a TextBox control for the class name called txtClassName, you can add a query string to your edit.aspx like this:
<asp:LinkButton ID="EditLink" runat="server" Text="Edit" PostBackUrl='<%# "edit.aspx?classname=" + txtClassName.Text %>' />
This creates the url in this format:
edit.aspx?classname=class1
Then, in the Page_Load event of your edit.aspx page, you can retrieve the selected class name like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string className = Request.QueryString["classname"];
// code to output the class details
// for example, if you want to add the class name to a dropdownlist:
DropDown1.Items.Add(className);
}
}

Changed text not posting back to code-behind

I have a textbox on an ASP.NET 4.0 page. I can add text to the textbox in the code-behind and when the page renders, the text displays just fine. However, when I modify the textbox in the browser and submit the page, the MyTextBox.Text property still shows the original text and not the modifed text I entered in the browser. How can the code-behind show the modified text?
You need to check for a postback in your initial textbox, like this:
if(!Page.IsPostBack)
{
MyTextBox.Text = "This is the text when page renders";
}
You can set your textbox normally in the code behind for the submit button handler.
void SubmitButton_Click(Object sender, EventArgs e)
{
MyTextBox.Text = tbUserInputHere.Text;
}
I doubt you took care of the page's IsPostBack condition properly.
You have to assign it a value like...
if (!IsPostBack)
{
TextBox1.Text = "You Text"
}
You are assigning a value in the page load event and when you click the button, the page load is called before the Click event handler and your value will be reset to the old value.

CheckedChanged EventHandler of dynamically added Checkboxes not firing inside UpdatePanel of a Repeater

I´ve read most posts here but i can´t figure out why the "CheckedChanged" Event is not firing. Here is my situation.
I´m using a Repeater to generate Items out of a Database. Each ReapeaterItem should include an UpdatePanel, because i have to Update the Controls inside the UpdatePanel and do not want to reload the complete page. Inside these dynamically generated UpdatePanels (each RepeaterItem has one) i´m adding up to three Checkboxes dynamically (based on the Database). These Checkboxes need to fire the "CheckedChanged" event, because on some conditions i want to enable/disable/check/uncheck Checkbox1, 2 or 3 based on business logic. ... Hope you got this so far. I´m adding all Controls and have the EventHandler Added. But the generated Code does not reflect the Event Handler. I tried OnItemDataBound, OnItemCreated, PreRender, ... Events to add the Eventhandler too, but i was not able to find the CheckBox-Control with the ID.
I´m totally lost with this and on the way to use Buttons instead of Checkboxes. From what i read so far is that with Buttons i can use the CommandName from the Button and the ItemCommand-Event from the Repeater to get a workaround, but then i need to reflect the "Check" on the Page in some way.
btw, every Repeater (8) sits inside an ajaxtoolkit-accordion control.
Here i give you some Code:
aspx-Page
<asp:Repeater ID="RepeaterAccordionPane2" runat="server">
<ItemTemplate>
HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Header")%>HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Beschreibung")%></td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode=Conditional>
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
HTML Stuff
</ItemTemplate>
</asp:Repeater>
Here is the Page_Load Part
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dvAlleArtikel = new System.Data.DataView(...Database...);
[... some other code here ...]
RepeaterAccordionPane2.DataSource = dvAlleArtikel;
//RepeaterAccordionPane2.ItemCreated +=new RepeaterItemEventHandler(RepeaterAccordionPane2_ItemCreated);
//RepeaterAccordionPane2.PreRender +=new EventHandler(RepeaterAccordionPane2_PreRender);
RepeaterAccordionPane2.DataBind();
int nUpdatePanelIndex = 0;
foreach (Control crInRepeater in RepeaterAccordionPane2.Controls)
{
if (crInRepeater.GetType() == typeof(RepeaterItem))
{
foreach (Control crInRepeaterItem in crInRepeater.Controls)
{
if (crInRepeaterItem.GetType() == typeof(UpdatePanel))
{
LiteralControl litTabelleBeginn = new LiteralControl("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleBeginn);
if (dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString() != "0")
{
CheckBox CheckBox1 = new CheckBox();
CheckBox1.ID = dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString();
CheckBox1.Text = (dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString() == "" ? "leer" : dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString());
CheckBox1.AutoPostBack = true;
CheckBox1.CheckedChanged +=new EventHandler(CheckBox1_CheckedChanged);
LiteralControl litNeueTabellenZeileBeginn = new LiteralControl("<tr><td width=10><img src=\"images/helper/spacer.gif\" width=\"10\"></td><td height=\"20\">");
LiteralControl litNeueTabellenZeileEnde = new LiteralControl("</td><td width=\"100\" height=\"20\">" + dvAlleArtikel[nUpdatePanelIndex]["ArtPrice1"].ToString() + " € </td></tr>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileBeginn);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(CheckBox1);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileEnde);
}
[... some other code here...]
LiteralControl litTabelleEnde = new LiteralControl("</table>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleEnde);
nUpdatePanelIndex++;
}
}
}
}
This code is never reached:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int foo = 0;
}
This is the CheckBox-Code generated:
<input id="AccordionPane2_content_RepeaterAccordionPane2_ctl00_6200" type="checkbox" name="AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200" onclick="javascript:setTimeout('__doPostBack(\'AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200\',\'\')', 0)" />
The Event is generated, but when i click the Checkbox all Content in the UpdatePanel is gone and the CheckedChanged-EventHandler is not fired.
What am i doing wrong?
Thanks to all advice, i´m really stuck.
mk
The first time the page loads you are adding all the checkboxes to the Controls collection, and they get rendered. When you do a postback (through the CheckBox's AutoPostBack) you have a check if(!IsPostBack) that doesn't allow the checkboxes to be added to the Controls collection on the postback. Because of that, you won't see the controls and the page, and when the page lifecycle tries to call the events (which occurs AFTER Page_Load), the controls that created the events are no longer there.
You will need to refactor your Page_Load method so it does two things - 1, regardless of the value of IsPostBack bind the repeaters and create the dynamic controls. 2, if IsPostBack==false, i.e., an initial load, then set the values of the dynamic controls. you don't want to set the values of the dynamic controls when IsPostBack==true because then you will lose the values the user entered.
also, just a note:
if (crInRepeater.GetType() == typeof(RepeaterItem))
can be rewritten as:
if (crInRepeater is RepeaterItem)

How do I target a GridViewRow with an AJAX.Net Toolkit PopupExtender?

I have a lot of data to display in a GridView. Because there's so much information per row, I'd like to be able to display additional information when a user clicks on the row, so I thought a PopupExtender from the AJAX Toolkit would be perfect.
Ideally, I want the popup to display whenever any of the controls within the row are selected. I've been able to successfully attach the PopupExtender to a single control within the row, but I can't get the pop-up to attach to the row itself.
I would have thought that setting the PopupExtender's TargetControlId to the Row's ClientID within the RowDataBound event would work, but when I do this I get a runtime error:
TargetControlID of 'popupExtId' is not valid.
A control with ID 'gvList_ctl02' could not be found.
I noticed that the GridViewRow is rendered, the tr element does not include an id, so I also tried extending the GridView control to override the CreateRow method to render the id - using this method I was able to render the row's ID (e.g. gvList_ctl02), but the same runtime error was thrown when I added the PopupExtender back into the code.
I also tried binding the showPopup() javascript command to the row's onclick event to get the popup to display manually; whilst the click event is registered OK and is definitely triggered, the popup is still not shown.
Does anyone have any idea how to / if you can bind a PopupExtender to a GridViewRow?
My row bound code is as follows:
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Bind the popup extender's target ID to the row ID
// This will cause a runtime error
PopupControlExtender pop = e.Row.FindControl("popupExtId") as PopupControlExtender;
pop.TargetControlID = e.Row.ClientID;
// Also bind the client side click handler to try to get the popup to show
// The alert is triggered and no javascript error is generated, but the popup does not display
e.Row.Attributes.Add("onclick", "alert('Row Clicked'); $find('" + pop.BehaviorID + "').showPopup();");
}
}
Many thanks.
If you're not opposed to using an ajax ModalPopupExtender, I use a little bit of javascript and some sneaky hidden button clicks to fire off my modal popups from within a grid view. I usually make my modal popup extender's target control id my hidden button, then, via javascript, fire my hidden button's click event to show the modal popup.
Here's my modal popup and hidden button markup.
<asp:Button ID="hiddenButton" runat="server" Text="" style="display:none"></asp:Button>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender2" runat="server"
TargetControlID="hiddenButton" PopupControlID="Panel1" CancelControlID="CancelButton"
BackgroundCssClass="modalBackground" Drag="True"/>
Here's my javascript to show my popup.
function showModal(btnID) {
btn = document.getElementById(btnID);
btn.click();
}
In my rowdatabound event, I call the javascript function showModal from button's onclick event.
Button myButton = (Button)e.Row.Cells[9].Controls[1];
matchButton.Attributes.Add("onclick", "showModal('" + hiddenButton.ClientID + "');");
Hope this might help point you in the right direction.

When I try to add a dynamically created control to an asp I get a error

When I create a control dynamically and add it to the page's controls collection, I get the following error. What's going on? How do I add controls to a page dynamically?
Control 'ctl02' of type 'TextBox' must be placed inside a form tag with runat=server.
I'm doing something like:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
double total = (double)ViewState[cKeyTotal];
TextBox txt = new TextBox();
txt.Text = "hello world";
this.Controls.Add(txt);
}
You don't have a form on the page. That control needs to be inside one.
Add a form:
<form runat='server' id='form1'>
...
</form>
And life should be good.

Resources