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
Related
I know that this question was asked many times before but I couldn't find an answer which solves my problem.
I need to do something very basic and simple, I have a GridView which has a template field and I am trying to access a cell text in the GridView,
So I have tried the following:
C#
Label lbl = GridView1.SelectedRow.Cells[0].FindControl("lblSomething") as Label;
string customerName = lbl.Text;
html
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:TemplateField SortExpression="Item">
<HeaderTemplate>
<asp:LinkButton ID="lblSomething" runat="server" Text="title" CommandName="Sort" CommandArgument="Something" ForeColor="white"></asp:LinkButton><br />
<asp:TextBox runat="server" ID="Something" AutoPostBack="false" Width ="60" autocomplete="off"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<%#Eval("Something") %>
</ItemTemplate>
<ItemStyle Width="80px" />
</asp:TemplateField>
lbl returns null.
Can someone please explain to me how to use FindControl? Try to be as clear as you can.
Your LinkButton is in the HeaderRow, not a normal row. You need to use FindControl on the header.
LinkButton lb = GridView1.HeaderRow.FindControl("lblSomething") as LinkButton;
And make sure you cast the right type. You are searching for a Label, but lblSomething is a LinkButton.
I'm using GridView and inside that I have DropDownList
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:DropDownList ID="drop_mail" runat="server" OnSelectedIndexChanged="drop_mail_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Dropdownlist is already populated by values(dynamically) and if user will select some other value from dropdownlist I would like to get that value without postback action.
I hope you will help me.
Thanks
"I would like to get that value without postback action."
If you dont want postback at all , you can catch dropdown change event in jquery.
<asp:DropDownList class="dropdown" ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
Note class 'dropdown' above.
$(document).ready(function(){
$(".dropdown").change(function(){
var selectedValue= $(this).val();
$(".ddl-value").val(selectedValue);
});
});
EDIT:
You can put one hidden variable in page like
<input type="hidden" id="selectedvalue" runat="server" class="ddl-value"/>
on server side access it like :
selectedvalue.Value
Add a hidden field with each dropdownlist and set the hidden field value to dropdownlist value using javascript. On submit loop through hidden field and get the value.
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>
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.
i think this maybe an easy one...
I have a Gridview, bound to a dataSource with this query:
select userid, name, phone from users
I need to change the content of the Name cell to a link, like so:
User's Name
so OnRowDataBound i'm doing this:
e.Row.Cells[1].Text = "" + e.Row.Cells[1].Text + "";
It all works fine this way. My problem is that i don't want to display the UserId column in the Gridview, but when i attribute it (asp attribute or server-side) Visible="false" to the BoundField UserId, i can't capture it's value to build the Name Cell.
Thanx for your help
Make your gridView to use template items instead of simple grivdiew column.
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName")%>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You can find a nice intro Here
Agree with Jani, this should be done with a template field. If you really need to do it the other way, see Can data be kept in a dynamically bound GridView's invisible fields?.
you can set the visibility on the onrowdatabound event