I have a situation where I currently have a HyperLinkColumn control that I would like to modify to have a Label or simple text appear in the same column as the hyperlink. How do I acheive this please? Any Idea?
If this is DataGrid then you can handle the ItemCreated event and add code that adds a new control. For example:
<asp:DataGrid ... OnItemCreated="OnMyDataGridItemCreated" ... />
private void OnMyDataGridItemCreated(object sender, DataGridItemEventArgs e) {
Label textLabel = new Label();
textLabel.Text = "Hello!";
e.Item.Cells[3].Controls.Add(textLabel);
// Instead of "3" you might need to pick a different column
}
If you are using a GridView you could use a TemplateField. In there, you can have a hyperlink and a label, which are both databound. In the OnRowDataBound() Event you can decide which of the two controls should be visible to the user.
Edit:
I've written an article about different ways to bind data to a GridView and decided to add an example of a HyperLink and Label, whose properties are set in the RowDataBound-Event: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns
Related
I have gridview putting data from one table from database using stored procedure.
On click of Edit Button, post back is happen which refreshing the data on gridview and wrong row is shown in edir mode.
I have found the reason is that on click on EDIT link javascript:__doPostBack('GridView','Edit$0') is displayed at the status bar, which is a problem here. Edit$0 means the absolute value of the row and when postback get the data from data base wrong row is shown in edit mode...
I think the solution will be put the edit mode based on not row number but some unique value of the selected row for edit.
Please help if any one has answer to it.
If you're using a gridview I suggest using the native OnRowEditing="GridViewEditEventHandler"
and then in the code behind
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(v=vs.110).aspx
However, if for some reason you are creating an OnClick function when you press the edit button and then changing the Itemtemplate to show/hide values I would suggest doing
GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
from there you can do the .findcontrol to find the specific item you want ex:
TextBox txtbox = (TextBox)TaskGridView.Rows[RowIndex].FindControl("textBoxID");
Hope this helps
I am new to asp.net website developer.
In my website I use GridView Control.In the GridView row i place some controls like
TextBox,DropDownList.
I can display the values in GridView.
But my requirement is ,Get the values from TextBox and DropdownList Which are existed in GridView.
Please help me to go forward..
thank you,
bye..
You need to access them by row. This code project article explains it in detail.
TextBox tb = (TextBox)gridview1.Rows[0].FindControl("idOfTextBox");
It above statement will find control in the first row of grid which has id idOfTextBox and type is textbox.
You can directly get any value of any control by casting directly, without using an extra textbox or dropdown variable.
Example:
string val = ((TextBox)gridview1.Rows[0].FindControl("TextBox_ID")).Text;
Hope it helps :)
Subscribe the RowDataBound event(this will be fired on each row available in gridview) of gridview and access like stated below,
protected void grdBillingdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//these controls are available inside gridview
HiddenField hdnNagetiveAmt = (HiddenField)e.Row.FindControl("hdnNagetiveAmt");
DropDownList ddlFeeType = (DropDownList)e.Row.FindControl("ddlFeeType");
TextBox txtFeeDesc = (TextBox)e.Row.FindControl("txtFeeDesc");
Button btnUpdate = (Button)e.Row.FindControl("btnUpdate");
}
}
not only text box we can get values from all the controls that used inside gridview that placed by using itemTemplate or simply a Bound field .
you need to loop for each row to get the values
foreach (GridViewRow row in grd_popup_details.Rows)
{
//get control values
}
Refference link
I have a boundfield in a gridview that programatically receives a hyperlink for the content in it. It gets a new dataset after a drop down list index change. One of the columns of the dataset will apply links to fields with data and skip those without, you can see my logic for applying links below:
if (e.Row.DataItem != null && int.TryParse(e.Row.Cells[4].Text, out incidents))
{
HyperLink incidentsLink = new HyperLink();
incidentsLink.ForeColor = System.Drawing.Color.Blue;
incidentsLink.NavigateUrl = "~/somesite.aspx?no=" + stnNum + "&dt=" + date;
incidentsLink.Text = e.Row.Cells[4].Text;
e.Row.Cells[4].Controls.Add(incidentsLink);
}
This is applied OnRowDataBound for the gridview. Then I have another gridview that is wired to another drop down list. When either drop down list changes index it grabs a new dataset for the related gridview and fires a ajax update using an update panel.
What happens is when the second gridview updates it erases the links in the first gridview. It doesn't erase the text, that stays, but the text is no longer anchored to a link. All links made this way and put into gridviews lose their link properties, however fields created using asp:HyperLinkField are left unchanged. The problem is that stnNum and date are not a part of the dataset that is returned for the first gridview, so I have to add them as a link after the gridview is already built.
The only solution I can think of is to refire the function that adds links to the gridview each time the second gridview updates. Any other solutions would be helpful, or an explanation as to why my link is being erased would be great.
Try binding the GridView/DropDowns only if there is not any POSTBACK. I believe POST Back event is letting your gridview & Controls to loose their data somehow.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind your Grids & Dropdowns here on page load
}
}
Right now I have an ASP Table. I can add rows and cells to this table just fine. What I would like to do, is instead of the cell just displaying text, I would like to add a control. For example a Button.
Right now, my first thought on how to do this would be just to put the <ASP:Button ... as the .Ttext attribute of the table cell. But my gut tells me this wont work. Further more, I probably couldn't add a function to handle the button click.
Can someone help point me in the right direction on how to achieve this?
You need to add the control to the table cell. Just call the Controls.Add method on the cell and add your control. Below is a brief sketch that should point you in the right direction.
Button b = new Button();
c.Controls.Add(b);
The following assumes you have a blank ASP:Table on your page with some defined rows (just for show really).
protected void Page_Init(object sender, EventArgs e)
{
foreach (TableRow row in this.Table1.Rows)
{
foreach (TableCell cell in row.Cells)
{
Button btn = new Button();
btn.Text = "Some Button";
btn.Click += new EventHandler(btn_Click);
cell.Controls.Add(btn);
}
}
}
void btn_Click(object sender, EventArgs e)
{
((Button)sender).Text = "Just Clicked";
}
The question hangs on what the source is for your controls. Bar far, the most effective way to make this happen is through data binding, even if your data source is just the Enumerable.Range() function.
Failing that, you need to create an instance of your controls and add them to the Control's collection of the table cell they will belong in. You can just use the += syntax for adding event handlers. The trick here is that the code to create and add the button will need to run again on every postback, and it will need to run before the page_load phase of the asp.net life cycle.
I have a gridview and I would like to be able to programatically change the HeaderText of it's columns (probably in the DataBinding event). I know this can normally be achieve with something like this:
myGrid.Columns[0].HeaderText = "My Header Text";
However, the gridview in question is actually nested within another gridview (via template column). So I can't access it directly. I'm trying to use the FindControl method to access it, but so far that isn't working. Any thoughts?
Capture that child grid in RowDataboud event of parent grid and here you can change the header text
Suppose myGrid is Parent Grid and ChildGrid is Child grid..
OnRowDataBound="myGrid_RowDataBound"
protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView ChildGrid = (GridView)e.Row.FindControl("ChildGrid");
ChildGrid.Columns[0].HeaderText = "My Header Text";
.
.
ChildGrid.Columns[n].HeaderText = "My Header Text";
}
}
Capture a reference to the nested gv in the itemdatabound event of the topmost gv. You can then try to change your header on the nested gv reference there. If that doesn't do the trick you can always conditionally show/hide placeholders from within the nested gv itemdatabound event when e.item.listitemtype is header.
Change Header Text Of Gridview Using RowData Bound:
The answer is:
if (e.Row.RowType == DataControlRowType.DataRow){
Gridview1.Columns[0].HeaderText = "New Header Name";
}