Value is not found in boundfield in gridview? - c#

I am trying to assing value to hiddenfield on indexchange event of dropdownlist ! Actually the problem is when I am trying to update my record i can not find value of that hidden field ! Kindly give me solution or suggest any another option ! Thank You !
My grid view is
<asp:TemplateField HeaderText="LocCode" SortExpression="LocCode">
<EditItemTemplate>
<ajax:UpdatePanel ID="upEditsLocation" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlLocation" runat="server"
DataSourceID="sdsLocation"
OnDataBound="ddlLocation_DataBound"
DataValueField="LocCode" AppendDataBoundItems="false"
DataTextField="LocCode"
AutoPostBack="true"
onselectedindexchanged="ddlLocation_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="sdsLocation" runat="server" ConnectionString="<%$ ConnectionStrings:ccConnString %>"
ProviderName="<%$ ConnectionStrings:CCConnString.ProviderName %>" SelectCommand="Select LocCode from Location">
</asp:SqlDataSource>
</ContentTemplate>
</ajax:UpdatePanel>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%# Bind("LocCode") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
and my indexchange event is
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
hdloc.Value = ddlLocation.SelectedItem.Text;
}
And my hidden field is
<asp:HiddenField ID="hdloc" runat="server" />

From the code I can see the HiddenField is not part of your update panel. Hence if you assign any value to it, it will not reflect on client machine. Increase the scope of panel to include the hidden field, and then try.
OR you can try this solution from ASP.net Forum
Here is a small tutorial on update panel (MSDN)
Hope this helps you.

GridViewRow cancel = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)cancel.FindControl("lblid");

If you can't access hdloc from code behind, either is not added by Visual Studio on aspx.designer.cs (try delete it and add it back or change the id and then back to original value) or the hidden field is placed in other template of another binding control, which means you need to use ctrl.FindControl("hdloc") then cast to HiddenField.
Also you need to place this hidden field into an UpdatePanel with UpdateMode="Always".
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
hdloc.Value = (sender as DropDownList).SelectedItem.Text;
}
I'm sure that ddlLocation.SelectedItem.Text, like you use it, it gives a compilation error, because ddlLocation is not visible on code behind, since is inside of EditItemTemplate.

Related

Set dropdownlist value in hyperlinkfield href

I am using asp.net web forms and have following concern.
I have one aspx page having asp:DropDownList and Gridview with asp:HyperLinkField in each column.
So I want to set the value of selected DropDownList to hyperLinkField tag dynamically either code behind or inside the following code.
<asp:HyperLinkField Text="Merge" DataNavigateUrlFields="MemberId" Target="_blank" DataNavigateUrlFormatString='/MergeMember.aspx?memberid={0}&clubid={1}" />
As you can see I have bind the field called MemberId in {0} and now I want to set the selected value of dropdownlist in {1}. Setting {1} is just a demo to show where I really want to set the value of selected dropdownlist I have't implemented that yet.
If it is possible please guide how?
If it is not possible please guide me how I can achieve this using different approach?
Thanks in advance.
.aspx Page
<asp:DropDownList ID="ddlTest" runat="server">
</asp:DropDownList>
<asp:GridView ID="grdTesting" runat="server" AutoGenerateColumns="false" Width="98%">
<Columns>
<asp:TemplateField HeaderText="Job Number">
<ItemTemplate>
<asp:LinkButton ID="lnkTest" runat="server" Text='<% # Bind("Test") %>' ToolTip='<%# Bind("Test") %>'
OnClick="lnkTest_Click"></asp:LinkButton>
<asp:HiddenField ID="hdnId" runat="server" Value='<% # Bind("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs
protected void lnkTest_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((Control)sender).Parent.Parent;
HiddenField hdnId = (HiddenField)gvr.FindControl("hdnId");
Response.Redirect("frmTest.aspx?Id=" + Convert.ToString(hdnId.Value) + "&DropDownId=" + ddlTest.SelectedValue, false);
}
Note:-
I think you have to change your Logic.....
In above Example...I'm binding a Gridview with Linkbutton and Id
On click of link button you can redirect to other page(as per your Logic..)
with sending Query string value(Id and Dropdownlist value)
I'm using asp.net with c# 4.5
Get external dropdownlist value here

How to set gridview to edit mode on button click instead of using commandname

Hi all I am having a template field as follows with an itemtemplate
<asp:TemplateField HeaderText="Edit/Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick=lnkEdit_Click"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Generally instead of Click event we use to write CommandName="Edit" and on OnRowEditing event we will set gridview row to edit mode with the following code
protected void grdDemo_RowEditing(object sender, GridViewEditEventArgs e)
{
grdDemo.EditIndex = e.NewEditIndex;
bindGrid();
}
Instead of this I would to set gridview row to edit mode on link button click, how can we do that any ideas please
There are couple of other option available since you wish to ignore the commandname :)
Click anywhere to activate edit mode in gridview
Activate Edit mode based on ID - Datakey
Set the EditIndex property to the appropriate row and then ReBind the GridVIew again to it's DataSource.
protected void btnEdit_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
Google/Bing for more..
You can use edit item template as follows
the following is the sample aspx code
<ItemTemplate>
<asp:LinkButton ID="lblSubject" Width="100%" Height="100%" CommandName="Edit" ForeColor="Black" runat="server" Text='<%#Bind("Subject") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblSubject" runat="server" Text='<%#Bind("Subject") %>'>
</asp:TextBox>
</EditItemTemplate>

Databind fires after ItemUpdated event

I'm using an asp.net 4 web site project (C#) in VS2008 and I have a FormView with ItemUpdated event:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
FormView1.DataBind(); // adding this line even doesn't help
TextBox box = FormView1.FindControl("TextBox1") as TextBox;
box.Enabled = false;
}
But I can't figure out, why an extra "FormView1.DataBind()" or Render(?) happens AFTER the ItemUpdated event. The result is that my code in the ItemUpdated event gets like "overwritten" and the TextBox1 doesn't get disabled.
When I set a breakpoint to the last line "box.Enabled = false;" then I see that after the ItemUpdated event it jumps to the aspx page again and steps through the TextBoxes.
Disabling this TextBox1 from another GridView1_SelectedIndexChanged works fine.
Is there any way to see the "current lifecycle progress" in debugging?
EDIT:
To clarify the reasoning...
I have a GridView1 where selecting the item populates the abovementioned FormView1. The point is that I need to disable some of the TextBoxes in the FormView1 based on, for example, user access levels.
Selecting the item from GridView1 disables the TextBox1 just fine, but when I click the Update button on the FormView1 then all TextBoxes are enabled, even if I see in the debugger code running through the GridView1_SelectedIndexChanged() function. And after I re-select the gridview item, the correct TextBoxes are disabled again.
Using even this code:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" DefaultMode="Edit" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("col2") %>' />
<asp:Button ID="Btn1" runat="server" CommandName="Update" Text="Update" />
</EditItemTemplate>
</asp:FormView>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
GridView1_SelectedIndexChanged(sender, e);
}
Maybe I should disable my Textboxes via another event?
This doesn't make sense, please explain more about why you are trying to disable the TextBox, have you just left the ItemTemplate off in your question? or is it actually missing? if it's missing why?
The TextBox is in the FormView's EditItemTemplate, so will only be visible when the FormView is in edit mode. After clicking update or cancel the TextBox will no longer be rendered, and the ItemTemplate is rendered instead. So there should be no need to set the TextBox to be disabled.
EDIT
Ok since you edited your question. You need to use the OnDataBound event of the FormView, which occurs at the end of binding, and disable your TextBoxes at that point.
aspx
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1"
OnDataBound="FormView1_DataBound">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
aspx.cs
protected void FormView1_DataBound(object sender, EventARgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
Rather then using gridview selected Index change you can use DataBound event on formview, so your logic would fire everytime the formview is rebind.

DetailsView FindControl() returns null after some postbacks

I've been working for a long time with GridViews and DetailsViews, but yesterday I've come across a new scenario, which I quite do not understand.
I have a GridView with ImageButton (CommandName="Insert") which will change the mode of the DetailsView to Insert. Afterwards I'll look for a DropDownList inside that DetailsView and add some items dynamically. Works fine, but one first the first time I press that ImageButton. If I click on "Cancel" in the DetailsView and press the ImageButton again, the .FindControl() Method returns null. What life cycle problem am I facing here?
I've created this sample: (To make it run in your Visual Studio, just bind a DataSource to the DetailsView, otherwise it will not be rendered)
Markup:
<asp:GridView ID="gvCategory" runat="server" OnRowCommand="gvCategory_RowCommand">
<Columns>
</Columns>
<EmptyDataTemplate>
<asp:ImageButton ImageUrl="~/images/add.png" ID="ibAdd" runat="server" CommandName="Insert" />
</EmptyDataTemplate>
</asp:GridView>
<asp:DetailsView ID="dvCategory" runat="server" Width="150px" AutoGenerateRows="false"
AutoGenerateInsertButton="True" DataSourceID="LinqDataSource1">
<Fields>
<asp:TemplateField HeaderText="foo">
<InsertItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server" Width="150"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView><asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="WebApplication1.DataClasses1DataContext"
TableName="Categories"></asp:LinqDataSource>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.gvCategory.DataBind();
}
}
protected void gvCategory_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
this.dvCategory.ChangeMode(DetailsViewMode.Insert);
DropDownList _ddlCat = (DropDownList)this.dvCategory.FindControl("ddlCategory");
if (_ddlCat != null)
{
_ddlCat.Items.Clear();
_ddlCat.Items.Add(new ListItem() { Text = "-- empty --", Value = "-1" });
}
}
}
I have also tried using a ItemTemplate, and not a InsertItemTemplate, but this results in the same. After using the ChangeMode-Method the DetailsView.CurrentMode == InsertMode. The only thing I can think of is, that the markup is already generated for the ItemTemplate and changing the Mode to InsertMode can't affect the rendered markup, or something like this.
Does anybody have a solution to this? =)
I think you are on the right track. It's hard to tell without seeing all of the code, but basically any time you change the rendering mode of a row in a repeater-type control you need to rebind it so that it's re-rendered. The fact that FindControl is returning NULL means only one thing: THE CONTROL IS NOT THERE. Which means it was not rendered. You can verify this by looking at the control hierarchy.
So, in your handler for Cancel are you rebinding?

How can i access a control within a ListView once a button has been clicked?

I need to access a label control in a listview when I've clicked a button (that is on the same row)...
Does anyone know how to do this please? :(
See below for more of an insight...
ASPX Page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" Visible="false">Your vote has been counted</asp:Label>
<asp:Button ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
Code Behind:
protected void voteOnThis(object sender, EventArgs e)
{
Button myButton = (Button)sender;
Voting.vote(int.Parse(myButton.CommandArgument));
// Here i would like to access the 'label' lblDone and make this Visible
}
In this simple case, I should consider using Javascript (JQuery)
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" style="visibility:hidden">Your vote has been counted</asp:Label>
<asp:Button OnClientClick="showLblDone()" ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' OnClick="voteOnThis" />
</ItemTemplate>
now, define inside a script tag the showLblDone function:
<script>
function showLblDone (){
$(this).siblings('span').show();}
</script>
You can also call this function with a parameter if you want to show/hide on every click, or you can use .toggle() instead of .show().
In this case you must add a div (or a Panel) inside the ItemTemplate.
You need to hook into the listview row bind and add the information you want to have when clicked. Using this, you can add an attribute to the button that you read back on click, for example...
If you posted some actual code, I could probably help some more.

Categories