I am currently developing a custom role/permission system...
I have a user control with all my permissions already and i want to re-use it for my custom role editor.
So right now I have a dropdownlist of the custom roles and a textbox to edit the name. What I want to do is...everytime a new value is selected in the dropdownlist the page to refresh and re-load my permission control with the RoleID and RoleName properties to be set to the DDL.SelectedValue and the textbox.Text like so...
<RSIPD:Permissions ID="Permissions" RoleID="[lstBaseRoles.SelectedValue]" RoleName="[txtCustomRoleName.Text]" runat="server" />
I know I am probably going about this the wrong way as I am more familiar with MVC3 than traditional ASP.NET Webform and am still learning.
This can be done in the code behind:
Permissions.RoleID = lstBaseRoles.SelectedValue
Permissions.RoleName = txtCustomRoleName.Text
This code would need to be called in the SelectedIndexChanged event of your DropDownList, and the TextChanged event of your TextBox.
Related
I'm a .NET newbie, but here is my question:
I have an application that makes use of Master Pages. I have a page that has a formview on it used to input address information. In the insertitemtemplate is a usercontrol that I am referencing. This usercontrol is a dropdownlist that lists countries. The usercontrol has public properties to get and set the selected value. The usercontrol is not databound, but static. The id of the usercontrol in the user control .ascx files is "DropDownListCountry". When I reference it on the formview in the page, the ID is "fvDropDownListCountry".
When I am executing the command to save the user input of the formview, I want to get the selected value that the user has indicated of which country they have selected. For the other input areas, which are text boxes, I am able to use the code such as:
Dim street1 As String = CType(myFrmView.FindControl("fvTextBoxStreet1"), TextBox).Text
Quite obviously, this approach doesn't work for a drop down list which is inside of the user control. I can't forexample use:
Dim country As String = TryCast(myFrmView.FindControl("fvDropDownListCountry"), DropDownList).SelectedValue
I've looked all around to try to find the answer on how to get the selected value from this user control, but being new at this, haven't found it yet. I have looked at functions to recursively examine a control for child controls, but haven't been able to make this work.
Any help would be appreciated
Where are you trying to get this value? An asp.net TextBox <input type=text /> automatically maintains state but this is not true of an asp.net DropDownList <select />
If you are attempting to access the DropDownList during PageInit, that value will not have been loaded yet as ViewState is not available. You need to either access it in PageLoad or as part of the SelectedIndexChanged event on the DropDownList.
FYI: Pretty sure that you can set the SelectedIndex or SelectedValue properties during PageInit but you cannot read that value.
this is my application link please download
i had 2 different user controls in one .aspx page.
first user control was dropdownlist binded with database.
second user control was gridview.
if i select a company int the dropdownlist products of that company should be displayed in the second user control.
my problem was, when i am selecting the companies in the dropdownlist gridview user control is loading first and 2nd dropdownlist selected event is firing..
please help me...
You should probably check the IsPostBack property and only populate the controls if it isn't a postback.
void Page_Load()
{
if (!IsPostBack)
{
// Populate the user controls.
}
}
But since you didn't show any code I'm just guessing.
I have the following situation:
I coded a aspx app c#, the user has 4 dropdownlists, one textbox and two buttons (cancel, save) in a page. I need the user to be remembered to save any changes to the textbox before allowing him to change the index of any dropdownlist. So, if the user changes the textbox value, he only have the option to cancel or save those chhanges. If he tries to do something else, like changing the index of a dropdownlist, I need to cancel this event and give him a message to save or cancel before do this.
I've tried many ways, but they all seem amatours to me and give lots of colateral efects. Is there any decent/elegant way to do this?
Create a custom validator for the TextBox. In the DropDownList's SelectedIndexChanged event handlers check the status of the TextBox and set the validator's args.IsValid property appropriately - you can notify the user via the TextBox CustomValidator ErrorMessage property to click Save if the value of the TextBox has changed.
I am working on ASP.NET application where I am reusing a user control. The user control contains a checkbox and bunch of other controls. I want to display all the controls inside the user control on all the pages but on one single page I want to hide the checkbox.
I was thinking that I can use the databind methods and see if I am on the "pagex" then hide the checkbox. Is there any other way to solve this problem?
If you have access to the code for the control you should be add a new property to the control for hiding/showing the checkbox and then just pass in the property depending upon what page you are on. You'd have to pass in the show/hide property on the load event of the page.
Do you have access to the code??
I would specify a parameter in the code behind for your ascx file. Example:
public bool HideCB = false;
THen when you put your User Control on your aspx page do this:
<uc:TestControl id="TestControl" runat="Server" HideCB="true" />
This way you can do a check on if(HideCB) to determine if you want to make it visible or not.
I've a following requirement for my asp.net page:
User can add a textbox dynamically on a page A by clicking on link "Add a new category" hyperlink
He clicks submit button on page A and gets redirected to page B.
When he clicks on page A link from this page, the textboxes that he added should be persisted.
Can someone help me with the code on this?
Appreciate your help!
In the ButtonClick Method write.
TextBox tb = new TextBox();
Parent.Controls.Add( tb );
The Parent is the control you want to add the textbox to, for instance a panel.
You can also look at this resource.
Hope it helps.
Adding a user control dynamically is simple. But in this case, I don't think you need to do that, instead you should look at creating a repeater with a textbox inside it, and when the user clicks Add Category, add one item to the repeater datasource.
This way you can handle both control creation and state persistence at the same time.
dealing with dynamic user controls can be a pain in the ass.
as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.
hope this helps.
Dynamically creating Textboxes:
suppose you have page like this
when you enter '1' in textbox and click on ADD button,output will be like display one textbox
below..
i have designed like this,
i have one textbox and placeholder for displaying dynamic textboxes..
double click on Add button...in btnadd_click,you have to write the following code
protected void btnadd_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < Convert.ToInt32(txtno.Text); i++)
{
TextBox txtbox = new TextBox();
phtxt.Controls.Add(txtbox);
phtxt.Controls.Add(new LiteralControl("<br>"));
}
}
debug it... and the output is,
As others have stated adding the text box dynamically is fairly straight forward, just create the Textbox and add it to the controls collections wherever you need it to show up. You then need to store the information that this user gets this additional text box. Assuming that this is meant for long term, you will need to store this information in your backend store. Whenever you are constructing the page you will need to read the store information first to see what textboxes to create.
I would suggest doing it as follows. In the Onload event, if you have not done so before, load the dynamic information from your DB. Add any necessary controls to the page and store this information in viewstate. On any subsequent postbacks, read the information from viewstate to add the additional controls. This will save you from having to read constantly from the database on each postback.