Getting an Instance of Dropdown list when clicking update on GridView in Asp.Net - asp.net

I have a GridView with more than 30 columns. Most are plain controls but for some I have added a template control (DropDownList, Calendar and CheckBox control). Here is the aspx code for the control in question
<asp:TemplateField HeaderText="Field1 Caption" SortExpression="Field1">
<ItemTemplate>
<asp:Label ID="lblConstructionArea" runat="server" Text='<%# Eval("Field1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlField1" EnableViewState="true" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
I wanted a dropdown to be shown on the column when a user clicked on Edit. So I add this code (and the above EditItemTemplate)
protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
{
string fieldOne = CommonUtils.ExtractControlValue(e,"lblField1",gvData);
gvData.SelectedIndex = e.NewEditIndex;
gvData.EditIndex = e.NewEditIndex;
gvData.DataBind();
BindGridDropDownData(e, CommonUtils.GetConstructionAreas() ,"ddlConstructionArea", constructionArea, "Field1", fieldOne);
}
In the above code I am getting the current available and passing it to another method so that when the dropdown is displayed the selected index can be shown accurately. After this I do a change on the dropdownlist and click on the "Update button" on the GridView and the following event is triggered
protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowEditIndex = e.RowIndex;
GridViewRow gRow = gvData.Rows[rowEditIndex];
DropDownList ddlConstructionArea = (DropDownList) gvData.Rows[rowEditIndex].FindControl("ddlConstructionArea"); //This does not work
ddlConstructionArea = (DropDownList)gRow.FindControl("ddlConstructionArea");//This does not work
ddlConstructionArea = (DropDownList)gvData.Rows[rowEditIndex].Cells[7].FindControl("ddlConstructionArea");//this does not work either
gvData.EditIndex = -1;//this works and the text boxes disappear
gvData.DataBind();//this works and the old data shows up on the gridview
}
I am curious as to how to do an update on a Grid where I have the binding is runtime.

Actually in the markup, you have given ID of dropdownlist as ddlField1 and in codebehind, you are reffering it as ddlConstructionArea. Is this is what causing the update not to function?

The problem was with the way the grid was being bound. I had written code to refresh the grid on page load and anytime I clicked on the Edit button the page was being refreshed and the grid binding was getting triggered instead of the updating event. I removed the code refreshing the grid on page load and put it in the places where it is needed and the order of events were getting triggered the way I would want it to and the update worked perfectly without a problem.

Related

asp.net binding query output into dropdown list in gridview

this is for ticketing system where the tickets are in different status like 'open, rejected, closed, resolved..'
In asp.net i have a gridview and in the gridview there are textboxes and a dropdownlist.
i can get output from the database and get to populate it in the gridview and also the contents in the dropdown list are also displayed.
how can i get to bind the data of the dropdownlist in the grid to that of the output of the query. if the ticket is closed it should select closed, if the ticket is rejected it should select rejected.
in the onrowCommand, i fetch data from another table and populate into the dropdown list. this is the full list of status like 'oopen, rejected, closed, resolved..'
Your question is missing details like what markup you have used for gridview and also what code you have for RowDataBound event.
So, I have provided a sample answer that you can easily adapt to your situation.
I am assuming you have a gridview with an id of GridView1 that contains the following ItemTemplate ( other columns have been omitted since they are not needed to understand this approach).
You should have a hidden field in the template so you can store the current status of the row; also when you are binding your gridview you should be getting a column called Status for the row.
Sample Markup
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowDataBound="GridView1_OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText = "Status">
<ItemTemplate>
<asp:HiddenField ID="hfStatus" runat="server" Value='<%# Eval("Status") %>' />
<asp:DropDownList ID="ddlStatus" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Then in your RowDataBound event where you are binding the dropdown for the row, you need to get the value of hidden field hfStatus and then set the selected item of dropdown using this value.
I have assumed that a method called GetStatusDropDownListData is there that gets the data for binding dropdown list.
Sample RowDataBound event
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlStatus = (e.Row.FindControl("ddlStatus") as DropDownList);
ddlStatus.DataSource = GetStatusDropDownListData();
ddlStatus.DataTextField = "Status";
ddlStatus.DataValueField = "Status";
ddlStatus.DataBind();
//Select the Status in DropDownList
string currentStatus = (e.Row.FindControl("hfStatus") as HiddenField).Value;
ddlStatus.Items.FindByValue(currentStatus).Selected = true;
}
}
i had used this code and its working now:
in asp file:
' Visible = "false" />
in the .cs file
string currentStatus = (e.Row.FindControl("lblStatus") as Label).Text;
ddlStatus.Items.FindByValue(currentStatus).Selected = true;

why a multiline textbox (inside a user control) is not holding its value after a postback?

I have a user control that have a gridview, buttons and a multi-line textbox for comments.
When the page posts back, Gridview is behaving normaly (its controls keeps their values after postback). However, the Comment textbox is always empty, no matter what I do.
protected void Page_Load(object sender, EventArgs e)
{
//This code is in the user ciontrol.
if (!IsPostBack)
{
string test = this.txtDepartmentComments.Text;
}
}
I put a break point at that line and the value is always empty. I've tried also to set the value in the code behind like:
protected void Page_Load(object sender, EventArgs e)
{
//This code is in the user ciontrol.
if (!IsPostBack)
{
this.txtDepartmentComments.Text = "Test!";
}
}
But when the page loads, the control remain empty.
Any idea why this is hapenning?
EDIT
This the ascx code (i.e. user control)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" >
</asp:GridView>
<asp:TextBox ID="txtComments" runat="server" Columns="45" TextMode= "MultiLine"/>
<asp:Button ID="btnComplete" runat="server" Text="Completed"/>
And thid id the aspx (i.e. the parent page)
<asp:Repeater ID="rpNewHire" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<user:MyUserControl ID = "MyUserControl1" runat = "server"
DepartmentID= '<%# Eval("DepID")%>'><user:MyUserControl />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
if(!IsPostBack) is for reading values from controls after postback, you are trying to read text box value on initial load which will always be empty, remove the '!' from your condition. Also it is hard to debug your issue with out the aspx page contents, try posting the design part as well in your question.
EDIT
Your user tag is not well formatted inside repeater, it is missing closing tag and runat attribute
<user:MyUserControl runat="server" ID = "MyUserControl1" DepartmentID= '<%# Eval("DepID")%>'></user:MyUserControl>
Controls inside repeater cannot be accessed directly, you have to loop through the rows of the repeater and find your child controls and then try reading values from them, that after you bind some data to the repeater.
Check this http://msdn.microsoft.com/en-us/magazine/cc163780.aspx
Page's IsPostBack and each user controls' IsPostBack are not same.
When a user control is posted back, that particular control's IsPostBack is true, but other user controls' IsPostBack are still false.
You can use explicitly !Page.IsPostBack inside a user control to check whether its parent page is posted back or not.
if(!Page.IsPostBack){
// do something
}
Dynamically Loaded Control can not maintain values at PostBack? It is not directly related to your question, but it might be useful for you.

Controls within .ascx are not displaying their new value on postback

This seems like an elementary issue but it has me stumped.
I have a main page which loads a custom control (.ascx) on page_load.
This custom control has two fields. One of them being a dropdownlist and the other being a text box. When the dropdownlist changes value it triggers a post back, some logic is executed server side and a value is generated for the textbox. I stepped through the code and the value is created and assigned correctly. However, when the page is rendered the value does not change.
If I change the textbox value to "QQQ" and trigger the postback, "QQQ" stays in the textbox so I can verify viewstate is working.
Is there any reason why the generated value is not being displayed in the form on postback. This process works fine on the initial page load.
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string ascxPath = #"~/_CONTROLTEMPLATES/TRC_BITS.ascx";
TRC_BITS control = Page.LoadControl(ascxPath) as TRC_BITS;
phForm.Controls.Add(control);
}
.ascx
<asp:TextBox ID="message" runat="server" TextMode="MultiLine" /><br/>
<asp:DropDownList ID="year" runat="server" AutoPostBack="true">
<asp:ListItem Text="2011">2011</asp:ListItem>
<asp:ListItem Text="2012">2012</asp:ListItem>
<asp:ListItem Text="2013">2013</asp:ListItem>
</asp:DropDownList>
.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
year.SelectedValue = DateTime.Now.Year.ToString();
}
if (year.SelectedValue == 2012)
message.Text = "ABC";
else
message.Text = "XYZ";
}
Because you're adding these controls to the page dynamically, you need to assign an ID to the user control. Make sure to assign the same ID to the controls every time the page is posted back, and the fields will be repopulated from ViewState.
Also, as Shai suggested, it would be more appropriate if you loaded the controls during OnInit instead of Page_Load. The situation is a little different with user controls, but in general you want to add your dynamic content before the LoadViewState method is executed.
If you're looking for something to take the pain out of persisting dynamic content, I would suggest taking a look at the DynamicControlsPlaceHolder.
Because you are adding the controls dynamically, you need to add them during the page's oninit event.
Try it, believe me. Go for it. Yalla.

LinkButton - bind field to ToolTip or CSSClass

I have a legacy asp.net 3.5 application. I need to bind a filed to CssClass so that i can utilize it via jquery.
Basically, in the datagrid, there are 2 buttons. Button one is visible and button two is not visible. On click of button one, i want to perform action and then make button two visible and hide button one. How can i do this? I just need a kick in the right direction...
<asp:LinkButton ID="lnkDelete" runat="server"
ToolTip="Delete Order <%# DataBinder.Eval(Container.DataItem, "TransID")%>"
OnClientClick="return DeleteOrder();"
OnClick="OrderDelete" CommandArgument='<%# Eval("TransID")'
CssClass="">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/icons/delete.gif"
BorderStyle="None" />
</asp:LinkButton>
My current binding inside the tooltip results in an error, "the server tag is not well formed".
On the code behind OrderDelete, i can disable the delete link, but how can i make the other button visible?
//delete indivisual order
protected void OrderDelete(object sender, EventArgs e)
{
string transactionID = String.Empty;
LinkButton lnkDelete = (LinkButton)sender;
if (lnkDelete != null)
transactionID = lnkDelete.CommandArgument;
if (!String.IsNullOrEmpty(transactionID))
{
//do delete
}
//refresh results
}
For the server tag not well-formed error, try something like this:
ToolTip='<%# String.Format("Delete Order {0}", DataBinder.Eval(Container.DataItem, "TransID")%>'
For the second part of your question, a little more of your code might help to give you a more specific answer, but in lieu of that, if you know which row of the DataGrid you're in, you should be able to do a FindControl in that row for the second button and make it visible.
Update
You might try setting the tooltip in the codebehind, using the RowDataBound event. Something like this:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = e.Row.FindContorl("lnkDelete") as LinkButton;
// You'll need to retrieve the values you want to dynamically populate
// the ToolTip with from other controls in the row;
// I don't know if you'd be able to use the DataSource or not, but you might.
btn.ToolTip = "Delete Order ";
}
}

Add new row to database table from Datagrid

I am trying to update a database table from my datagrid using an event handler and ItemCommand. I manage to call the routine and everything is working fine except the text that is inserted into my database is empty. I managed to track this back to the text not being passed from my datagrids footer to the sql parameters. I tried using a string first and then passing that to the parameters but they were also empty. I am accessing the control using the following line.
sqlcmd.Parameters.Add("#GoodsDesc", SqlDbType.VarChar).Value = CType(e.Item.FindControl("txtGoodsDesc"), TextBox).Text
The control itself is defined using
<asp:TemplateColumn HeaderText="Goods Descriptions">
<ItemTemplate>
<asp:Label runat="server" ID="lblGoodsDesc" Text='<%# Eval("GoodsDesc") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtGoodsDesc" runat="server" TextMode="MultiLine" Rows="3"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
Am I missing something here? It's like the text in the footer isnt being tied to the control before I call it.
HI Ryan, we will need more code then just that. Where are you .Add +ing this parameter in addition where is e.Item.FindControl what event is it?
You need to check if you are on the footer control:
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (dg.EditItemIndex != -1)
{
((TextBox)e.Item.FindControl("txtGoodsDesc")).Text
}
}
}
Or in vb.net
if (e.Item.ItemType = ListItemType.Footer) then
Dim s as String=String.Empty
s=CType(e.Item.FindControl("txtGoodsDesc"), TextBox).Text
end if
So essentially I discovered that the issue that was occurring was that VB.Net was calling Page_Load before the event itself was being called, and in the Page_Load method I was calling the method that filled the datagrid, which cleared the textboxes in the footer before the values in them were being read.
To stop this I placed a condition around the call to the method in Page_Load
If Not IsPostBack Then
FillDataGrid()
End If
Then I called the FillDataGrid() function as the very last step in the handler, meaning that the data was read in and then the datagrid was bound to the new values.

Resources