I have a gridview which displays all a database table with columns TaskId, Title, Reward, Time Allotted, Poster Name. Although all my SqlConnection code is present on another webform which is used to insert data in to database table.
Here is my code on InsertTask.aspx.cs page:
protected void btnPost_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spTasks", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Title", txtTitle.Text);
cmd.Parameters.AddWithValue("#Body", txtBody.Text);
cmd.Parameters.AddWithValue("#Reward", txtRewards.Text);
cmd.Parameters.AddWithValue("#TimeAllotted", txtTime.Text);
cmd.Parameters.AddWithValue("#PosterName", txtPoster.Text);
con.Open();
cmd.ExecuteNonQuery();
lblStatus.Text = "Task Posted Successfully.";
}
}
This inserts task in to database table successfully. So on a new webform which is default.aspx, I have a gridview which is connected to that database table and shows a list of tasks in the table.
Again, I have not written any code on default.aspx.cs.
What I need to do is make Title as a hyperlink in the gridview which I can click and get on a different page which is in accordance to the rows. Can that be done? Or should I use a button on all rows to get to other pages accordingly. How can that be done?
I have no experience with gridview.
<telerik:GridTemplateColumn HeaderText="Action">
<ItemTemplate>
<a id="A1" runat="server" href='<%# #"~\Demolink.aspx"+( Eval("Record_ID").ToString()) %>'>
<%#Eval("Title ") %> </a>
</ItemTemplate>
</telerik:GridTemplateColumn>
According to your requirement get all data to a dataset or datatable and bind it to gridview. And in default.aspx markup we can convert gridview columns to hyperlink fields in different manners by using <asp:TemplateField><ItemTemplate></ItemTemplate></asp:TemplateField> in <Columns> tag
Here I added two controls one is html hyperlink and other is asp:hyperlink
<asp:GridView runat="server" ID="gvrecords" AutoGenerateColumns="false" DataKeyNames="Title">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("Title") %>' NavigateUrl='<%# Bind("Title", "~/../urpath/{0}") %>' runat="server"/>
<a href ='<%#"page.aspx?TitleID="+DataBinder.Eval(Container.DataItem,"TitleId") %>'> <%#Eval("Title") %> </a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Yes of course you can use hyperlink for title. To provide hyper link you can use TemplateField as mentioned in below code sample :
<asp:GridView runat="server" ID="gridTask" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Eval("Title") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Body" HeaderText="Body" />
<asp:BoundField DataField="Reward" HeaderText="Reward" />
</Columns>
</asp:GridView>
Note: Here I have used stackoverflow site url. instead you can use the url of your page.
<asp:TemplateField>
<ItemTemplate>
<%#Eval("Title") %>
</ItemTemplate>
</asp:TemplateField>
This should work!
Related
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
I have a gridview with template fields.Now all template fields are showing in the left side of gridview. How can I set it to the right side? My gridview is
<asp:GridView ID="GridView1" BorderWidth="1px" CellSpacing="18" CellPadding="10" runat="server">
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server">Single OT</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server">Double OT</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And also binding a query with this gridview. My Query is
qry="select OT from OverTime where EmpCode=#EmpCode"
dr=conn.query(qry);
GridView1.DataSource = dr;
GridView1.DataBind();
Youcan use the ItemStyle-HorizontalAlign attribute:
<asp:TemplateField ItemStyle-HorizontalAlign="Right">
For particular Template Column you can add ItemStyle tag like,
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Right" Width="150px"></ItemStyle>
</asp:TemplateColumn>
so that the content within that particular Template Column gets aligned towards right
Hope this helps!!
I am importing a column in datatable to my grid. Now I want navigate to a new page on selecting a cell in grid by fetching the selected value. I have tried this by including bound field in my grid like
<asp:GridView ID="GDV_1" runat="server" EnableModelValidation="true" AutoGenerateColumns="false" OnSelectedIndexChanged="GDV_1_SelectedIndexChanged" AutoGenerateSelectButton="false" DataKeyNames="SROID">
<Columns>
<asp:BoundField DataField="SRONameinEnglish" HtmlEncode="False" DataFormatString="<a target='_blank' href='Test.aspx?code={0}>ClickHere</a>" />
</Columns>
</asp:GridView>
Doing this way my requirement is achieved but the all cells are displaying Common text Click here instead of showing data from Database.
Please give your suggestion on how to get the value from database into cell and make it clickable. I don't want to use Select button. Please find my current output.
This is my current output I want my data from DB instead of ClickHere.
You can use TemplateField
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnk<%# Eval("SRONameinEnglish")%>"><%# Eval("SRONameinEnglish")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and click of LinkButton put your code to navigate anywhere.
In your case you are binding boundfield with static a tag which have href attribute so your not able to change text on that boundfield from your database.To get your approach you should
use TemplateField and bind data with text attribute using eval keyword as below example.
Try it:
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
OR
you can also bind link with your hyperlink using NavigateUrl property of hyperlink as below example.
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink id="HyperLink2" NavigateUrl='<%#Eval("YourUrl") %>' Text='<%#Eval("name") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
I hope it will helpful to you.
I have a gridview which contains template fields in which every template field contains a litreal control, and I want to bind that grid view with my DataSet, please look into the code to find more.
Code to create DataSet-
DataTable Record = new DataTable();
Record.Columns.Add("zerker");
DataRow dr = Record.NewRow();
dr["zerker"] = "SomeText";
Record.Rows.Add(dr);
gvCustomres.DataSource = Record;
gvCustomres.DataBind();
Code to create GridView-
<asp:GridView ID="gvCustomres" runat="server" PageSize="4" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Zerker">
<ItemTemplate>
<asp:Literal ID="zerkername" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
Please help me to find correct way to do this.
Thanks,
Your code above is all right if you just want to bind Column "zerker" to your gridiview.
All you are missing is the Text Property for your Literal control.
<asp:Literal ID="zerkername" runat="server" Text='<%# Eval("zerker") %>'>
</asp:Literal>
I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
For each row in your GridView there is a HiddenField for the ProductID.
You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):
HiddenField hiddenFieldProductID =
(HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");
string productID = hiddenFieldProductID.Value
// Do something with the value
Hope, this helps.
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.