How to get the values which are there in edit item template? - asp.net

I have a control placed in edit item template in detail view control. what ever the control I am using I need to access those values in my code behind page that in my item updating event
Can any one tel me the syntax how to acces it those control values?
Actually what ever control are there in my edit item template are not added in my designer.cs file what is the issue for this
thank you

this will definitly work try..
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
TextBox txtbox = (TextBox)DetailsView1.FindControl("YourControlID");
}

Related

Asp.net go to previous page

I have 5 dropdownlist in asp.net.User selects 5 dropdownlist after that click to button.Button sends page to another page.If i go previous page dropdownlist selectedvalues and datas lose(it displays default values without selected values)
I tried below
Response.Redirect("PreviousPage.aspx");
datas losing is there any solution ?
Instead of using Response.Redirect(), you can change the PostBackUrl of the button to the target page, or use Server.Transfer(). Once there you should be able to access the properties you need from the Page.PreviousPage object.
Example using Server.Transfer:
Page1.aspx.cs:
protected void SubmitButton_Click(object sender, EventArgs e)
{
Server.Transfer("Page2.aspx");
}
Page2.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
var addressDropdownSelectedValue = ((Page1)Page).PreviousPage.AddressDropdown.SelectedItem.Value; //or SelectedValue
}
With the ScriptManager in the AJAX control toolkit (you get this in 4.0+, I think maybe even in 3.5), you can add history points. You add a history point and use that value to rebuild the page state.
http://msdn.microsoft.com/en-us/library/cc488548%28v=vs.140%29.aspx
You can also strongly type the previous page if you want to grab values from it. You do this with <%# PreviousPage %> directive. Then in code you could use Page.PreviousPage.FindControl

asp.net dynamic user control button click issue

I made researching about this subject I could not find proper answer.
In my default.aspx page, I have a treeview. Codes are in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Control ucont;
if (TreeView1.SelectedNode.Value == "Yeni Dönem")
{
ucont = LoadControl("usercontrols/yenidonem.ascx");
PlaceHolder1.Controls.Add(ucont);
}
else
{
ucont = LoadControl("usercontrols/tabloktar.ascx");
PlaceHolder1.Controls.Add(ucont);
}
}
I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ?
Thanks.
First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.
In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.
If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.
I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.

ListView insertitem template

I have one listview that compiles layout template, item template and insert item template. Rest is working fine except insert item template hide/show functionality. I have a requirement to hide or show the insert item template, what i have tried till now is..i took one asp button and wrote the following line on click event.
lvwCustomers.InsertItemPosition = InsertItemPosition.None
It does nothing on the first click but it disables the insert item template on the second click and after that my submit button gives object reference exception.
Any Help.
Like this?
protected void Button1_Click(object sender, EventArgs e)
{
ListView1.InsertItem.Visible = false;
}

ASP .Net Gridview rowupdated - can it happen before the page load?

Here's the scenario:
-Gridview control
-Calendar control
I only want the calendar to show if a specific item is chosen in the drop down list which is in a gridview. When the grid view row is updated I want to change whether or not the calendar is visible. The calendar's visibility only shows correctly on the next post back.
Page_Load is called before events which are called before Render. There is no reason why you couldn't, in your event, check the value of the dropdownlist and set the Calendar control visible property, this would then knock into Render.
Try adding a check of IsPostBack before setting the loading your GridView. That will prevent you from overwriting it's values.
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) {
/*Populate your GridView*/
}
}
protected void GridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
/*show your calendar here if you need to*/
if(whatever) calendar.Visible = true;
}
This should work, if it doesn't then I'd recommend putting breakpoints in your Page_Load and RowUpdated methods and stepping through it, preferrably with a Watch on the gridview's datasource (it'll go red if it's changed) and a watch on calendar.Visible, to help you see if something has changed.
For the record, control events like OnRowUpdated will never fire before Page_Load unless explicitly called for some reason. Chances are you're just doing something where it's not updating the content of the GridView before it gets to the RowUpdated method, or it's overwriting the data in the GridView due to a lack of !IsPostBack check.

Postback destroys user controls in my GridView columns

I have a ASP.NET GridView that uses template columns and user controls to allow me to dynamically construct the datagrid. Now I'm implementing the event handler for inserting a row. To do that, I create an array of default values and add it to the data table which is acting as a data source. However, when my OnLoad event is fired on postback, all my template columns no longer have the user controls. My gridview ends up just being all blank with nothing in it and my button column disappears as well (which contains the add row, delete row and save buttons).
My row add event just does this:
public void AddDataGridRow()
{
List<object> defRow = new List<object>();
for (int i = 0; i < fieldNames.Count; i++)
{
defRow.Add(GetDefaultValueFromDBType(types[i]));
}
dt.Rows.Add(defRow);
}
It is fired from a button in a user control that's implement like this:
protected void Button1_Click(object sender, EventArgs e)
{
((Scoresheet)(this.Page)).AddDataGridRow();
}
My on load event does a bunch of stuff on first run to set the GridView up but I don't run that again by using the IsPostBack property to tell.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Initialize();
}
Anyone have any hints as to why my user controls are vanishing?
You have to add the controls to the grid on every page_load, not just if it's (!Postback)
Do you have the EnableViewState=true on the usercontrols and the GridView?
Is the AddDataGridRow() method called by Initialize()? You basically have two options:
Bind the grid on every postback and do not use viewstate (performace loss)
Bind the Grid only the first time (if (!IsPostBack)), and make sure that your user controls keep their viewstate.
From your code, it is not clear whether the user controls keep viewstate and what they have in them. It is not even clear what is the execution order of the methods you've shown. There is no binding logic, so even if you keep adding rows, the grid may still not be bound. Please elaborate a bit and show the whole page codebehind.

Resources