how to make hyperlink in gridview without datafield? - c#

Developing In: c# asp.net web forms 4.5
I've looked at many posts and all of them made a hyperlink with either datafield in it, by datatextfield() or similar.. or called eval from backend.
I didn't do neither of that, so I tried doing every thing I could but it didn't work. CommandField, HyperLinkeField, HyperLink, .. all didn't work.
The main point is that the data comes out fine, but I can't seem to make it hyperlink. Is it impossible to have a hyperlink this way?
I'm trying to make a hyperlink on gridview.
The thing is that the column I'm trying to make the hyperlink
doesn't get data from the datafield.
It gets it by onRowDataBound method of gridview
something like this..
<asp:GridView runat="server" ID="someGrid" CellPadding="10"
DataKeyNames="idx" AutoGenerateColumns="false"
selectMethod="someGrid_GetData" ItemType="someTable"
updateMethod="someGrid_UpdateItem" AutoGenerateEditButton="true"
deleteMethod="someGrid_DeleteItem" AutoGenerateDeleteButton="true"
onRowDataBound="someGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="thing1" HeaderText="thing1" />
<asp:BoundField DataField="thing2" HeaderText="thing2"/>
<asp:DynamicField DataField="poDate" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="vendor" />
<asp:CommandField HeaderText="sku" ShowSelectButton="true" SelectText="{0}" ButtonType="Link"/>
</Columns>
</asp:GridView>
and on the code background, it goes like this..
protected void soGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
using (soAction soa = new soAction())
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string s = e.Row.Cells[2].Text;
string thin1 = soa.get_thin1(s);
e.Row.Cells[4].Text = thin1;
string thin2 = soa.get_thin2(s);
e.Row.Cells[5].Text = thin2;
}
}
}
Thanks!
edit :
Maybe I wasn't clear about what is the challenge here..
normally the examples use dataTextField property in gridview but
I cannot use the dataTextField property because I'm binding the data
depending on the model rendered afterwards.
I'm doing this because I wanted to set the itemType into someTable
so I can use the updatemethod and deleteMethod of the asp.net.

Just use a TemplateField like this, you can do anything you want in a templatefield, combine data from multiple columns, create controls, whatever:
<Columns>
<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>
</Columns>
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs

Related

LinqDataSource does not support the Select property when the Delete

I have LinqDataSource that being attached to GridView In Asp.net web application.
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" OnInit="LinqDataSource1_Init"
EnableDelete="True"
EntityTypeName="Id" TableName="CompanysCourses"
OnDeleting="Course_Deleting" OnDeleted="Course_Deleted">
</asp:LinqDataSource>
Within OnInit event I have modified the select and where properties like that
this.LinqDataSource1.Where = "CompanyId == " + UserManager.CompanyId;
this.LinqDataSource1.Select= "Course";
I am selecting table named CompanysCourses and after that filter the result and select specific courses based on company's ID
And this is my grid view
<asp:GridView ID="grdVwCourses" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="Id" DataSourceID="LinqDataSource1"
OnSelectedIndexChanged="grdVwCourses_SelectedIndexChanged"
CssClass="table table-responsive"
PageSize="10" GridLines="Horizontal"
meta:resourcekey="GridView1Resource1">
<EmptyDataTemplate>
<asp:Label runat="server" ID="NotFoundLabel" Text="" meta:resourcekey="NotFoundLabelResource1"></asp:Label>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="#" meta:resourcekey="HeaderId">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" Visible="false"/>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" meta:resourcekey="BoundFieldResource1" />
<asp:BoundField DataField="EnglishName" HeaderText="EnglishName" SortExpression="EnglishName" meta:resourcekey="BoundFieldResource2" />
<asp:BoundField DataField="CreatedDate" HeaderText="CreatedDate" SortExpression="CreatedDate" meta:resourcekey="BoundFieldResource3" />
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<ItemTemplate>
<asp:HyperLink ID="userLink" runat="server" NavigateUrl='<%# GetCategoriesLink(Eval("Id")) %>' Text="Categories" meta:resourcekey="userLinkResource1" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" meta:resourcekey="CommandFieldResource1" />
</Columns>
</asp:GridView>
I have in the grid view enabled deleting but when click delete I got this exception
LinqDataSource 'LinqDataSource1' does not support the Select property
when the Delete, Insert or Update operations are enabled.
I have override On_Deleting event like that to show confirmation dialog
protected void Course_Deleting(object sender, LinqDataSourceDeleteEventArgs e)
{
e.Cancel = true
this.panMessage.Visible = true;
int id = ((Course)e.OriginalObject).Id;
ViewState["DeleteObject"] = id;
}
I wonder what is the reason of this problem.
Edit
The question referred to as duplicate to mine doesn't have the answer that I seek, also the details are different.
I came today to the same issue on some legacy project, as the error showing this widget doesn't support deleting when you use SELECT statement.
so the solution is to remove your select statement from this datasource, ie remove this line:
this.LinqDataSource1.Select= "Course";
then on the gridview change DataKeyNames to your table Id name and also change the navigation property to access the fields, for example Course.Id, Course.Name etc for each columns in the fields.
the last thing is to cast to the main table and use the any field to access the Id of that table child value.
int id = ((Course)e.OriginalObject).Id;

GridView adding column with button

I need to add column with button in GridView ? i tied with asp:button i got error also i tried asp:ButtonField i got this error:
"Error Creating Control - narudzbaGridType 'System.Web.UI.WebControls.ButtonField' does not have a public property named 'ID'.
but i gave ID name to my Button field asp:ButtonField ID="example"
<asp:GridView ID="narudzbaGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Let" HeaderText="Let"/>
<%--<asp:BoundField DataField="Kolicina" HeaderText="Kolicina"/>--%>
</Columns>
</asp:GridView>
You can use TemplateField like this (add to columns block):
<asp:templatefield headertext="Author Name">
<itemtemplate>
<asp:button id="buttonl"
Text= 'Click Me'
runat="server"/>
</itemtemplate>
</asp:templatefield>
Hi you need to add an TemplateField. Everybody like use ImageButton but if you want use other control go ahead.
<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:ImageButton ID="imgBtnEditar" runat="server" ImageUrl="~/iconos/Image.png" CommandName="edit" ToolTip="Edit">
</asp:ImageButton>
</ItemTemplate>
<ItemStyle Height="8px"></ItemStyle>
</asp:TemplateField>

How to select a cell in datagrid onClick in ASP.net C#

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.

How do I Pass Multiple Selected Datagrid Values to Another Page in asp.net?

I have multiple asp.net data grids that a user can select a checkbox to the corresponding value that they want, put in the amount they want in a textbox and submit the request. How do I show what values they requested with the amount on the next page? I can do this with a session I believe but I'm having a hard time finding good examples for something like this. Since they can select multiple values I can't use a query string right? I'm using VB.net but if you answer in c# that's fine. Thanks!
<asp:GridView ID="flexGridView" DataKeyNames="ID" runat="server" AutoGenerateColumns="False" DataSourceID="FormSqlDataSource" CssClass="gridView" ClientIDMode="Static">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="flexCheckBoxList" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Form" ShowHeader="False" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" ShowHeader="False" Visible="False" />
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty" >
<ItemTemplate>
<asp:TextBox ID="flexTextBox" runat="server" Width="40" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use Session or query string, but I would recommend Session, as there are limitations on the length of a query string, especially if you are not sure how many check boxes might be checked.
Use the OnCheckChanged event of the check box control and set AutoPostBack to true in your template field, like this:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="flexCheckBoxList" runat="server"
AutoPostBack="True" OnCheckedChanged="Check_Clicked" />
</ItemTemplate>
</asp:TemplateField>
protected void Check_Clicked(Object sender, EventArgs e)
{
// Store the check box name, ID or whatever unique value you want in Session here
CheckBox theCheckBox = sender as CheckBox;
// Was the check box found?
if(theCheckBox != null)
{
// Store in Session
Session["CheckBoxValue"] = theCheckBox.SomePropertyValue;
}
}
Then in the Page_Load of your redirect page, you will need to read out the Session value for the checked check boxes.

How can I show fewer than n columns in a grid view where it has n columns?

I have an ASP Grid View; its Datasource is a list of entities. The entity has 11 properties, but I want to show only some of the columns in the grid view.
My Grid View is
<asp:GridView runat="server" ID="GridForResult"
Caption="Update The Result for the Folloing Students"
Visible="true" ShowHeader="false">
<Columns>
<asp:TemplateField ItemStyle-Width="100px">
<ItemTemplate>
<%#Eval("TestRoll")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="120px">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="80px">
<ItemTemplate>
<%#Eval("Program")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In my code behind page, I have bound the grid like this:
List<FormGridEntity> gridEntities = new List<FormGridEntity>();
gridEntities = AdmissionResult_BAO.GetAllCandidateAdmissionInfo();
GridForResult.DataSource = gridEntities;
GridForResult.DataBind();
//What Will I need to do here or somewhere else
What should I do now?
#V4Vendetta has it right - in your GridView markup, set AutoGenerateColumns="false":
<asp:GridView runat="server" ID="GridForResult" AutoGenerateColumns="false" Caption="Update The Result for the Folloing Students" Visible="true" ShowHeader="false">
Alternatively, if you want to define columns in the markup and turn them off individually, you can set the Visible="false" property:
<asp:TemplateField ItemStyle-Width="100px" Visible="false">
You don't have to do anything in your code-behind, unless you want to turn columns on/off programatically:
GridForResult.Columns[0].Visible = false; // Turns off the first column

Categories