I have a gridview which pulls data from an xml file. One of the columns of the gridview is a hyperlinkfield. I want to bind a URL field contained in my XML file to this column. I think I have the right idea with the code below but cant figure out how to finish it. The URL is the datakey of the gridview by the way.
protected void grdContents_RowCreated(object sender, GridViewRowEventArgs e)
{
((HyperLinkField)grdContents.Columns[1]).NavigateUrl =
}
you can bind hyperlink on gridView_RowDataBound event like this
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
HyperLinkField lnkHyper = (HyperLinkField)e.Row.FindControl("HyperLinkField1");
lnkHyper.NavigateUrl="";
}
}
try this.
or you can also bind url using DataBinder.Eval at the time of binding source to grid like
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl="<%# DataBinder.Eval(Container.DataItem, "url") %>"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can use this as well, as you also providing data source to Grid view.
Related
So I have a GridView for a C# web application, that has a Buttonfield, and when that button is clicked, I need to get the value of one of the fields for that row and store it in a variable for processing in some way.
However, neither the GridView nor the ButtonField seem to possess any means of doing this.
Can anyone recommend a way of getting data from a GridView, or if this is not possible, a different type of view that does offer this functionality, while still displaying a whole table (eg, not a DetailsView)
You can Check this link: https://msdn.microsoft.com/en-us/library/bb907626(v=vs.140).aspx.
Define the CommandName of the Button.
In the GridView Define the RowCommand Event and Check the CommandName.
Get the Index of the Row.
Get the Column with GridView.Rows[index](columnIndex)
If you are using asp:TemplateField like shown below then you can access the row content using RowCommand
Markup
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("CustomerID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="AddCode" Text="Add New"/>
Code
protected void gvwSearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "AddCode")
{
var clickedButton = e.CommandSource as Button;
var clickedRow = clickedButton.NamingContainer as GridViewRow;
var rows_lblCode = clickedRow.FindControl("lblCode") as Label;
// now you can acccess all the label properties. For example,
var temp = rows_lblCode.Text;
}
}
I have the following:
<Columns>
...
<asp:BoundField DataField="datetime_added" HeaderText="When Added" ReadOnly="True" SortExpression="datetime_added" />
...
</Columns>
The DataField datetime_added comes from a SQL Data Source. I want to do some editing of every value for this column after the values are queried from the SQL Data Source on the C# side. Kind of like:
//short pseudo code example
protected void someEvent(object sender, EventArgs e)
{
sender.GetField["datetime_added"] = "test";
}
Is there a way to do this?
use a templatefield instead of a boundfield
- with a label
- the label text is fed by a function throught something like this:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text='<%= SomeFunction(Eval("db_id"),Eval("description")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And this in your code behind:
protected string SomeFunction(object id, object description)
{
//your code
return "";
}
When your function is called, the field from SQL is passed as an object
so that you can use it if you want or return something else based on whatever you want.
The function is called when the value is bound from the datasource in each row, you'll probably need to pass an id as well to identify the row.
Have you tried the "OnRowCreated" event? I'm assuming here that you're using a GridView, and if not, then there may be something similar in whatever component it is you're binding the data to.
For GridView, here is an example:
protected void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// do what you need in here
// e.Row.Cells[x] where x = the column number
}
}
Also, this method can be really slow depending on how many rows you're processing.
If you are using RowDataBound event to manipulate the values before it's being bound to the grid (getting called while binding every row to the gridview)
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string datetimeadded = DataBinder.Eval(e.Row.DataItem, "datetime_added").ToString();
// or you can use the cell index to reference the boundfield like below
string datetimeadded = e.Row.Cells[1].Text;
}
}
I have a GridView that draws fields from a dataset:
protected void Page_Load(object sender, EventArgs e)
{
var dat = SecurityManager.GetAllGroups();
GridView1.DataSource = dat;
GridView1.DataBind();
}
One of these is the IsAdministrator property.
It should show up as a checkbox.
It does, but it is grayed out. I want it to be enabled so I can modify it.
From there, I will go through each row and update accordingly.
Is this possible to do? How can I ungray the checkbox?
Thanks
If you are using the CheckBoxField, you can only change the value when in edit mode. To workaround this, if you want a checkbox for all rows, use the TemplateField, and supply a checkbox in the item template like:
<asp:TemplateField ..>
<ItemTemplate>
<asp:CheckBox .. Checked='<%# Bind("IsAdministrator") %>' />
</ItemTemplate>
</asp:TemplateField>
I created my gridview with checkboxes inside of it with this code.
<asp:GridView ID="GridView1" runat="server" Width="366px" autogeneratecolumn="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="SelectAllCheckBox" runat="server" AutoPostBack="true" oncheckedchanged="SelectAllCheckBox_OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="EachCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried check/uncheck it.
enter link description here
protected void SelectAllCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
String test = "test";
test = "newtest";
GridView1.DataSource = null;
GridView1.DataBind();
}
But it doesn't trigger any event.
enter link description here
I'm trying to find where my code is missing and searched so far but still can't.
Thank you for your help!
You must use OnItemCreated or OnItemDataBound and link your checkbox with your delegate
void Item_Created(Object sender, DataGridItemEventArgs e)
{
CheckBox cbx = (CheckBox)e.Item.FindControl("SelectAllCheckBox");
cbx.CheckedChanged += SelectAllCheckBox_OnCheckedChanged;
}
The code looks fine and works for me.
I suspect you might be binding the GridView on every postback.
When you click the CheckBox with the event attached it causes the page to refresh. If you bind the CheckBox on Page_Load (or any method that occurs on every trip to the server) it will bind the grid every time you click the CheckBox. In this case it will never get as far as firing your event.
If so, try checking for a postback before binding your GridView.
For example:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridview1.DataSource = myDataSource;
GridView1.DataBind();
}
}
I have two GridView in ASP.net 3.5 page. I have HyperLink field as one of the fields in the First GridView.
On click of this hyperlink I need to call display the 2nd grid by passing some values to a method showAllRecords(value from hyperlink)
How do I do this?
Thanks
You can try a TemplateField like this for GridView1 (primary GridView)
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="LinkButton1" CommandName="cmdName" CommandArgument='<%# Eval("IdColumn") %>' > LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and in GridView1's RowCommand, you can get the CommandArgument and setup the DataSource for GridView2 (child GridView).
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName = "cmdName")
{
var arg = e.CommandArgument;
// use arg to filter GridView2's DataSource
GridView2.DataSource = FilteredDataSource;
GridView2.DataBind();
// show GridView2 if it's hidden.
}
}
Maybe the following blog post could give you a hint;
http://www.tugberkugurlu.com/archive/parent-child-view-in-a-single-table-with-gridview-control-on-asp-net-web-forms
First you need to handle SelectedIndexChanged event on the first grid and then get the value from the hyperlink. Is the hyperlink a DataKey? If it is then you get it by GridOne.SelectedDataKey.Values["key"] otherwise get the actual cell by valuefromGridOne = GridOne.SelectedRow.Cells[num].Text where number is the cell number. Once you have it you can pass the value to your second grid by handling Selecting event of the objectDataSource (assuming you use it to bind data) and passing the value like this e.InputParameters["dataKey"] = valuefromGridOne;