I've been trying to fire the Checkbox OnCheckedChanged event of an ASPxCheckBox implemented in HeaderTemplate of code. But it isn't working.
Please find below code.
<dxwgv:GridViewDataTextColumn Caption="Select" FieldName="Included">
<Settings AllowAutoFilter="False" />
<HeaderTemplate>
<dxe:ASPxCheckBox ID="chkSelectAll" runat="server" AutoPostBack="True" AllowGrayed="false" OnCheckedChanged ="SelectAllOperation">
</dxe:ASPxCheckBox>
</HeaderTemplate>
<DataItemTemplate>
<dxe:ASPxCheckBox ID="chkSelect" runat="server" Value='<%# Eval("CompositeKey") %>'
Checked='<%# Eval("AccessCode").ToString() == "A" %>' OnInit="chkSelect_Init">
</dxe:ASPxCheckBox>
</DataItemTemplate>
<CellStyle HorizontalAlign="Center" />
</dxwgv:GridViewDataTextColumn>
Following is the function I expect to reach after checking the checkbox in HeaderTemplate.
public void SelectAllOperation(object sender, EventArgs e)
{
}
However, It is working fine when implemented in DataItemTemplate. When I select the checkbox in HeaderTemplate, OnCheckedChanged event is not fired and I'm unable to reach the required function.
Related
I Have a LinkButton In a TemplateField In a GridView. The LinkButton is bound to database which shows the time save in database.All I want is to fetch each linkbutton time value and compare it to current time and if linkbutton time is less than current time ,make it disable.
Any pointers will be helpful.
Thanks in advance.
<asp:GridView ID="grdview" AutoGenerateColumns="false" runat="server" OnRowCommand="grdview_RowCommand">
<Columns>
<asp:BoundField DataField="AudiName" DataFormatString="Audi {0}" HeaderText="Audi Name" />
<asp:TemplateField HeaderText="StartTime">
<ItemTemplate>
<asp:LinkButton ID="lnkmovietime" runat="server"
Text='<%# Eval("StartTime") %>'
CommandName="time"
CommandArgument='<%#Eval("AudiID") %>'>
</asp:LinkButton>
<asp:HiddenField runat="server" ID="hf1" Value='<%#Eval("StartTime")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use the OnRowDataBound event to hook into the data binding and disable the button.
Form
<asp:GridView ID="MyGridView"
OnRowDataBound="MyGridView_RowDataBound"
runat="server">
....
</asp:GridView>
CodeBehind
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Find the button
var linkButton = (LinkButton) e.Row.FindControl("MyLinkButton");
// Toggle enabled/disabled based on time
// Using the button text is probably a bad idea, but is used here for demonstration purposes
linkButton.Enabled = (Convert.ToDateTime(linkButton.Text) > DateTime.Now);
}
}
The code above has not been tested, but should give you an idea of how you can approach this.
You can set the Enabled property of LinkButton comparing with the current time:
<asp:LinkButton ID="lnkmovietime" runat="server"
Text='<%# Eval("StartTime") %>'
Enabled = '<%# ((DateTime)Eval("StartTime")< DateTime.Now )? false : true %>'
CommandName="time" CommandArgument='<%#Eval("AudiID") %>'>
</asp:LinkButton>
I have a Gridview with a column that has a DropDownList.
I've binded this Dropdownlist with an event on the "SelectedIndexChanged".
The problem is i can't get the value of a label of another column in the same row.
The code is the next:
protected void grid_OnSelectedIndexChanged(object sender, EventArgs e)
{
grdCredenciales.DataBind();
var dropdown = (DropDownList)sender;
var row = (GridViewRow)dropdown.NamingContainer;
var label = (Label)row.FindControl("lblMatricula");
var value = label.Text; // I get "" in this line.
}
And in the grid i have:
<asp:ObjectDataSource ID="CredencialesDS" runat="server" />
<asp:GridView ID="grdCredenciales" runat="server" BackColor="White" DataSourceID="CredencialesDS"
CssClass="DDGridView" RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6" AllowSorting="True"
AllowPaging="True" AutoGenerateColumns="False" PageSize="10" OnRowDataBound="grdCredenciales_OnRowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label7" ToolTip="MatrĂcula" runat="server" Text="MatrĂcula"/>
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left" Width="15%"/>
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblMatricula" runat="server"><%# Eval("Matricula") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label19" ToolTip="Estado" runat="server" Text="Estado" />
</HeaderTemplate>
<HeaderStyle HorizontalAlign="Left" Width="15%"/>
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:DropDownList runat="server" ID="dpEstadoCredencial" AutoPostBack="True" OnSelectedIndexChanged="grid_OnSelectedIndexChanged" CssClass="comboEstado"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I don't know why, but label.text returns an empty string. As you can see, i am calling the DataBind before, so the label should have a value at this point.
Do you know how can i get the value i need from the label in another column?
Thanks for everyone.
Check the GridView's DataSource before you do the DataBind(). Since you're missing the full ASPX markup, I'm not sure if you're setting the data source programmatically or with a SqlDataSource.
In any case, what will happen often with programmatically-set Data Sources is that they disappear on a PostBack, and when you call that DataBind, you're really DataBinding it to null, which would explain why you're getting string.Empty ("") for the Label's Text property.
Just verified the code provided by you. It's working completely.
Please make sure in the RowDataBound event of Grid View, you reattach the dropdownlist's SelectedIndexChanged event as below:
protected void CustomersGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Page.IsPostBack)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("dropdown1") as DropDownList;
if (ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(CustomersGridView_SelectedIndexChanged);
}
}
}
}
Also, I used the same code as yours in SelectedIndexChanged event. I'm putting here my aspx Markup:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
runat="server"
OnRowDataBound="CustomersGridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Label2" Text='<%# Bind("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="dropdown1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdown1_SelectedIndexChanged">
<asp:ListItem Text="Cat"></asp:ListItem>
<asp:ListItem Text="dog"></asp:ListItem>
<asp:ListItem Text="Mouse"></asp:ListItem>
<asp:ListItem Text="pig"></asp:ListItem>
<asp:ListItem Text="snake"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
Please provide your GridView markup too for checking.
After spending hours on it i still couldnt find the Solution,Plaese Help.
I am Developing a Website where I have a Gridview with data,i have a linkbutton as a templatefield in one of the Columns which onClick is doing a postback and displaying the records in a modalpoupextender,which is inside a seperate asp panel,
i have edit,update,delete buttons in that popup which are doing postback and functioning well.
Remmember i have not used UpdatePanel anywhere,as i do not have that kind of requirement.
Now,i have another LinkButton on the Page(not in gridview),which has some other functinality after OnClick postback,the problem is When i Click That LinkButton it first displays the ModalpopUp and then it does the postback and completes the function.The important part is that the Modal PopUp disappears just in seconds.
So,i just get a glimpse of Modalpopup whenever i click on the LinkButton.
I tried ModalPopUpExtender1.hide() at every Possible Place,but its not Working.
I am not sure how to disable that popup from appearing on LinkButton Click.Please Help me on it.I am in Trouble.
Thanks in Advance.
Here is my GridView Code:
<asp:GridView ID="dg_Task" runat="server" CssClass="gridview"
BorderWidth="1px"
Font-Names="Trebuchet MS" Font-Size="Small" AllowSorting="True"
AutoGenerateColumns="False" onrowdatabound="dg_Task_RowDataBound"
Width="100%" Height="143px"
onprerender="mergeDoc">
<Columns>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:Label ID="lbl_tid" runat="server" Text='<%#Bind("ID") %>'
Visible="False"></asp:Label>
<asp:LinkButton runat="server" ID="Lnk" ForeColor="Black"
CommandArgument='<%# Bind("ID") %>' Font-Underline="False" onclick="Title_Click"
Text='<%# Bind("Task") %>' Font-Names="Trebuchet MS"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="35%" />
<ItemStyle Height="2px" HorizontalAlign="Left" VerticalAlign="Top" />
</asp:TemplateField>
Here is The CodeBehind:
protected void Title_Click(object sender, EventArgs e)
{
ddown_status.Items.Clear();
var btnId = sender as LinkButton;
string id = btnId.CommandArgument;
btnUpdate.CommandArgument = id.ToString();
btnDelete.CommandArgument = id.ToString();
//Some Code which fills the details of my PopUp Fields
this.modalPopUpExtender1.Show();
}
Here is the another LinkButton(On Main Page) code:
protected void LinkButton1_Click(object sender, EventArgs e)
{
//here even i simple postback(Without any code) Triggers The PoPUp and Provides a Glimpse then the rest code runs
//sample code here
}
Please Help .Thanks
I'm working on a C#/ASP 4.0 project where I'm trying to make a shopping cart application.
There is a GridView on my Products page that shows all of the items, and I want the user to be able to click an "Add to Cart" button field in this GridView which will, obviously, add an item to their cart.
I'm having issues actually setting an OnClick event for the gridview, though? That doesn't seem to be available in the Event menu in the Properties. Additionally, I can't seem to figure out how to get the specific row, either. I have method that does this...
int productID = Convert.ToInt32(GridView1.Rows[n].Cells[0].Text);
AddToCart(productID);
But I have no idea how to figure out n, or how to have this method get called when they click that ButtonField in the gridview.
You could do this:
First, add a template field to the gridview:
<asp:TemplateField HeaderText="Add to Cart">
<ItemTemplate>
<asp:Button id="bthAddToCart"
CommandArgument'<%#Eval("ProductID")%>'
OnClick="bthAddToCart_Click"
Text="Add to Cart"
runat="server"/>
</ItemTemplate>
</asp:TemplateField>
Now, add the handler for the Click event of the button:
protected void bthAddToCart_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int productID = Convert.ToInt32(button.CommandArgument);
AddToCart(productID);
}
You can use the template fields like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="30px">
<ItemTemplate>
<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="AddToCart"
CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Data Found.
</EmptyDataTemplate>
</asp:GridView>
Then on your code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
int ProductID = Convert.ToInt32(e.CommandArgument);
AddToCart(ProductID);
}
}
Hope this helps! Good Luck!
Use the OnRowCommand event of the grid view. More details: here
You have to use OnRowCommand Event for Gridview. Use Following Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Header Text Here">
<ItemTemplate>
CONTROL TO SHOW COLUMN DATA
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="30px">
<ItemTemplate>
<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="AddToCart"
CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
In C# Code use following code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add To Cart")
{
int ProductID = Convert.ToInt32(e.CommandArgument);
AddToCart(ProductID);
}
}
Here's my code in a gridview that is bound at runtime:
...
<asp:templatefield>
<edititemtemplate>
<asp:dropdownlist runat="server" id="ddgvOpp" />
</edititemtemplate>
<itemtemplate>
<%# Eval("opponent.name") %>
</itemtemplate>
</asp:templatefield>
...
I want to bind the dropdownlist "ddgvOpp" but i don't know how. I should, but I don't. Here's what I have, but I keep getting an "Object reference" error, which makes sense:
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) //skip header row
{
DropDownList ddOpp = (DropDownList)e.Row.Cells[5].FindControl("ddgvOpp");
BindOpponentDD(ddOpp);
}
}
Where BindOpponentDD() is just where the DropDownList gets populated. Am I not doing this in the right event? If not, which do I need to put it in?
Thanks so much in advance...
Ok, I guess I'm just dumb. I figured it out.
In the RowDataBound event, simply add the following conditional:
if (myGridView.EditIndex == e.Row.RowIndex)
{
//do work
}
Thanks to Saurabh Tripathi,
The solution you provided worked for me.
In gridView_RowDataBound() event use.
if(gridView.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
{
// FindControl
// And populate it
}
If anyone is stuck with the same issue, then try this out.
Cheers.
I had the same issue, but this fix (Jason's, which is adding the conditional to the handler) didn't work for me; the Edit row never was databound, so that condition never evaluated to true. RowDataBound was simply never called with the same RowIndex as the GridView.EditIndex. My setup is a little different, though, in that instead of binding the dropdown programmatically I have it bound to an ObjectDataSource on the page. The dropdown still has to be bound separately per row, though, because its possible values depend on other information in the row. So the ObjectDataSource has a SessionParameter, and I make sure to set the appropriate session variable when needed for binding.
<asp:ObjectDataSource ID="objInfo" runat="server" SelectMethod="GetData" TypeName="MyTypeName">
<SelectParameters>
<asp:SessionParameter Name="MyID" SessionField="MID" Type="Int32" />
</SelectParameters>
And the dropdown in the relevant row:
<asp:TemplateField HeaderText="My Info" SortExpression="MyInfo">
<EditItemTemplate>
<asp:DropDownList ID="ddlEditMyInfo" runat="server" DataSourceID="objInfo" DataTextField="MyInfo" DataValueField="MyInfoID" SelectedValue='<%#Bind("ID") %>' />
</EditItemTemplate>
<ItemTemplate>
<span><%#Eval("MyInfo") %></span>
</ItemTemplate>
</asp:TemplateField>
What I ended up doing was not using a CommandField in the GridView to generate my edit, delete, update and cancel buttons; I did it on my own with a TemplateField, and by setting the CommandNames appropriately, I was able to trigger the built-in edit/delete/update/cancel actions on the GridView. For the Edit button, I made the CommandArgument the information I needed to bind the dropdown, instead of the row's PK like it would usually be. This luckily did not prevent the GridView from editing the appropriate row.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/delete.gif" AlternateText="Delete" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Delete" />
<asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/edit.gif" AlternateText="Edit" CommandArgument='<%#Eval("MyID") %>' CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/update.gif" AlternateText="Update" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Update" />
<asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/cancel.gif" AlternateText="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
And in the RowCommand handler:
void grdOverrides_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
Session["MID"] = Int32.Parse(e.CommandArgument.ToString());
}
The RowCommand, of course, happens before the row goes into edit mode and thus before the dropdown databinds. So everything works. It's a little bit of a hack, but I'd spent enough time trying to figure out why the edit row wasn't being databound already.
protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (grdDevelopment.EditIndex == e.Row.RowIndex && e.Row.RowType==DataControlRowType.DataRow)
{
DropDownList drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("ddlBuildServers");
}
}
Try this one
This will help u
This code will be do what you want:
<asp:TemplateField HeaderText="garantia" SortExpression="garantia">
<EditItemTemplate>
<asp:DropDownList ID="ddgvOpp" runat="server" SelectedValue='<%# Bind("opponent.name") %>'>
<asp:ListItem Text="Si" Value="True"></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("opponent.name") %>'></asp:Label>
</ItemTemplate>