I have a GridView containing a TextBox in <asp:TemplateField /> and The GridView is residing inside an AJAX Update Panel.
I want to register the TextChanged Event for the textbox inside the GridView but only for the first row inside the Grid.
Is there a way to do it?
I tried binding the OnTextChangedEvent and AutoPostBack = true for TextBox, but it is firing for textbox in each row. How can I limit that TextChanged Event to only the TextBox in first row in the GridView.
Can you please help me.
Thanks and appreciate your feedback.
Perhaps you can use GridView_RowDataBound event and instead of attaching event to TextBox in aspx markup, do it via code behind:
protected void view_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
{
TextBox txtBox = (TextBox)e.Row.FindControl("txtBoxId");
txtBox.TextChanged += new EventHandler(txtBox_TextChanged);
}
}
}
Related
I have a data grid with four columns which includes 3 text column and a checkbox. Clicking on checkbox of different rows then click on delete button needs to remove the checked rows from data grid.
But the checked event not firing up for the first time when i clicked on Delete button but the next time it fires and the checked value of the checkbox is true.
It would be grateful if i have a solution for this.
thanks in advance.
Note : I have used Data Grid not Gridview and i don't have row property.
I have added a checkbox as Item template inside a Data Grid.
<asp:CheckBox OnCheckedChanged="id_CheckedChanged" runat="server" Visible='<%# DataBinder.Eval(Container, "DataItem.DeleteCheckboxVisible") %>' ></asp:CheckBox>
addded OnCheckedChanged event in code behind.
protected void id_CheckedChanged(object sender, EventArgs e)
{
CheckBox lnkView = (sender as CheckBox);
if (lnkView.Checked)
{
Response.Write("you checked the checkbox");
}
else if (!lnkView.Checked)
{
Response.Write("checkbox is not checked");
}
}
In the button Click Event
protected void ButtonDelete_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
foreach (DataGridItem row in DataGridEmployeeLifeEvents.Items)
{
if (row.ItemType == ListItemType.Item)
{
CheckBox chkVjezba = (CheckBox)row.FindControl("CheckBoxDelete");
if (chkVjezba.Checked)
{
//something
}
}
}
}
`````````````
If I just drop a gridview on my page (AutoGenerateColumns = true) and then wire up a datasource on the code-behind, is there a way to get a click event to occur in each cell for certain columns? I know I can add buttons,checkboxes, etc from designer. I was just wondering if I can do it quick and easy with AutoGenerateColumns on.
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataControlFieldCell c = e.Row.Cells[2] as DataControlFieldCell;
//Maybe somehow put a click event here.
}
}
You can add client-side onclick event and pass cell's (TD) ID there:
c.Attributes["onclick"] = "myJSfunction('" + c.ClientID + "')";
And perform some logic on client side, calling server if needed either via some Button/LinkButton click() method, calling __doPostBack() on some other control or even making AJAX call.
I have a UserControl which contain GridView.I set AutoGenerateSelectButton is true for this GridView.
But it didn't work when I press Select inside UserControl.
Do you have anyidea?
You should move your event handling to the page that owns the UserControl and not in the UserControl
Inside the Page_Load of your page, add this
myUserControl.FindControl("GridView1"));
dvGrid.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
Add the handler to the page
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//access the GridView
GridView grid = (GridView) sender;
//access the selected row
GridViewRow selectedRow = grid.SelectedRow;
//access the selected Primary key - make sure you set the DataKeyNames property of the GridView to the Record Id - in your Markup
string currentRowPrimaryKey = grid.SelectedValue;
//OR
string currentRowPrimaryKey = grid.SelectedDataKey.Value;
}
Now you have several values to play with. You can put a break point and examine the properties of the sender to have more options. Good Luck
I'm having trouble figuring out how could I use my imagebutton (see below) in my ITemplate to append the button's corresponding row data (ItemID) as a query string.
My ImageButton in my ITemplate:
ImageButton select_button = new ImageButton();
select_button.ID = "select_button";
select_button.ImageUrl = "~/Files/System/Icons/highlighter.png";
select_button.CommandName = "Select";
select_button.ToolTip = "Select";
container.Controls.Add(select_button);
Should I handle it in in the imagebutton's OnClick event (if so, is there a way to get the row where the button is located) or can I handle in in the GridView events (rowbinding, rowseleted, rowcommand, etc.)?
I'd be glad to elaborate more on my code upon request. ^ ^
You can set the ID in the CommandArgument property of your button control in the RowDataBound Event. Once you have an ID, you can track rows with it.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
((Button)e.Row.FindControl("select_button")).CommandArgument = dr["IdColumn"].ToString();
}
}
Is it possible to have a Checkbox that only shows up when Editing the last row of a GridView?
I have tried something like this in the EditItemTemplate:
<asp:CheckBox ID="chkNextDay" runat="server"
ToolTip="Is it a next-day departure?"
Enabled="true"
Checked='<%# DateTime.Parse(Eval("OutHour","{0:d}")).Date >
DateTime.Parse(Eval("InHour","{0:d}")).Date %>'/>
Then on code-behind I tried hiding it for rows other than the last one like this:
protected void grvOutHour_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView grvOutHour = (GridView)this.grvReport.Rows[grvReport.EditIndex].FindControl("grvOutHour");
TextBox txtBox = (TextBox)grvOutHour.Rows[e.NewEditIndex].FindControl("txtEditOutHour");
CheckBox nextDay = (CheckBox)grvOutHour.Rows[e.NewEditIndex].FindControl("chkNextDay");
if (grvOutHour.Rows.Count-1 != e.NewEditIndex)
nextDay.Visible = false;
}
This ALMOST worked, but the checkbox kept showing for all fields, I think because the RowDataBound is called AFTER RowEditing again so it renders the whole thing again :(
Any suggestions?
Thanks,
EtonB.
Use RowDataBound instead...
protected void grvOutHour_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
GridView grid = (GridView)sender;
CheckBox nextDay = (CheckBox)e.Row.FindControl("chkNextDay");
nextDay.Visible = (e.Row.RowIndex == (grid.Rows.Count - 1));
}
}
You will need to handle hiding the checkbox in the RowDataBound event.
You'll need to determine what the last row is, and set the checkboxes visible property to true when that condition is true, obviously.
I guess it's more of a hack than an elegant solution, but I would probably just hide the other checkboxes via JavaScript if the condition is true.