asp:Button is not calling server-side function - c#

I'm instantiating an asp:Button inside a data-bound asp:GridView through template fields. Some of the buttons are supposed to call a server-side function, but for some weird reason, it doesn't. All the buttons do when you click them is fire a postback to the current page, doing nothing, effectively just reloading the page.
Below is a fragment of the code:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" CssClass="l2 submissions" ShowHeader="false">
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:Panel ID="swatchpanel" CssClass='<%# Bind("status") %>' runat="server"></asp:Panel></ItemTemplate>
<ItemStyle Width="50px" CssClass="sw" />
</asp:TemplateField>
<asp:BoundField DataField="description" ReadOnly="true">
</asp:BoundField>
<asp:BoundField DataField="owner" ReadOnly="true">
<ItemStyle Font-Italic="true" />
</asp:BoundField>
<asp:BoundField DataField="last-modified" ReadOnly="true">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="viewBtn" cssclass='<%# Bind("sid") %>' runat="server" Text="View" OnClick="viewBtnClick" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The viewBtn above should call the viewBtnClick() function on server-side. I do have that function defined, along with a proper signature (object,EventArgs). One thing that may be of note is that this code is actually inside an ASCX, which is loaded in another ASCX, finally loaded into an ASPX.
Any help or insight into the matter will be SO appreciated. Thanks!
(oh, and please don't mind my trashy HTML/CSS semantics - this is still in a very,very early stage :p)

Is AutoWireupEvents set to true?

If your GridView is being databound during Page_Prerender, the asp:button in question doesn't exist when postback events are processed.
Try binding the gridview during Page_Load, or using the GridView's OnItemCommand event rather than the button's OnClick.

Related

How to change column attributes in GridView?

I have a GridView that has 4 data fields and 1 hidden template field holding 2 buttons. I'm trying to programmatically set the template visibility to true, but the following doesn't work:
aspx
<asp:GridView ID="PendingView" runat="server" AutoGenerateColumns="False" OnDataBound="CheckButtons">
<Columns>
<asp:BoundField DataField="RID" visible="false" />
<asp:BoundField DataField="ExpenseType" HeaderText="Expense Type" />
<asp:BoundField DataField="CurrentDate" HeaderText="Date" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:BoundField DataField="Details" HeaderText="Details" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" text="Approve" Visible="false" />
<asp:Button ID="btnDecline" runat="server" text="Decline" Visible="false"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs
//all works perfectly fine except last line
DataSet allInfo = new DataSet();
allInfo = GetData();
PendingView.DataSource = allInfo;
PendingView.DataBind();
PendingView.Columns[5].Visible = true; //doesn't work
Your template field is not hidden, the two buttons inside it are what's hidden. So of course setting the column visibility to true doesn't work. You need to change the visibility of the buttons inside.
It looks like you're trying to make it visible right at the beginning, so why are you hiding the buttons in the aspx page and then trying to make it visible in the code-behind? Simply make them visible in the aspx page by removing the Visible="false":
<asp:Button ID="btnApprove" runat="server" text="Approve" />
<asp:Button ID="btnDecline" runat="server" text="Decline" />
If you really want to hide it in aspx and show it in the code-behind, then hide the template field itself in the aspx page:
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" text="Approve" />
<asp:Button ID="btnDecline" runat="server" text="Decline" />
</ItemTemplate>
</asp:TemplateField>
Then your code-behind will work as you have it in the question.
If you want to actually keep the aspx page as you have it in the question (with the buttons hidden), then you need to show the buttons, not the template, in the code-behind. The problem is that to access those buttons, you'll have to iterate through the rows. Something like this:
foreach(var row in == PendingView.Rows) {
row.FindControl("btnApprove").Visible = true;
row.FindControl("btnDecline").Visible = true;
}

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>

GridView Template Issue with Javascript

Im working in ASP.net with C#, i need help to correct or have an approach to get what i need.
Im working with gridview, data select a set of data that will be used depending of its primarykey into another table.
On the runtime on client side, i need to collect on the onclick event, the sid column of each row and put it into a hidden field. However, the code below, is not working for me as the <%#eval("sid"); %>! is being read as a string, than the current row value.
What i need is the checkbox once clicked alert(5) instead of alert('<%#eval("sid"); %>!'); that is what is currently doing.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbSelect" onclick="javascript:alert('<%#eval("sid"); %>!');"/>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataTextField="nombre" NavigateUrl="http://www.google.com" HeaderText="direccion"/>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="nombre_archivo" HeaderText="nombre_archivo"
SortExpression="nombre_archivo" />
</Columns>
If further information is needed, please ask what you need me to add to the question.
Try this, worked for me:
<asp:CheckBox runat="server" ID="cbSelect" onclick='<%# "javascript:alert(" + Eval("sid") + " );" %>'/>
I believe Eval is case-sensitive, and you don't need that semi-colon:
<%# Eval("sid") %>
Try changing your code to this. And your Eval() was actually eval() which is invalid.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server"
ID="cbSelect"
onclientclick='<%#string.Format("javascript:alert('{0}');",Eval("sid"))%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataTextField="nombre"
NavigateUrl="http://www.google.com" HeaderText="direccion"/>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="nombre_archivo" HeaderText="nombre_archivo"
SortExpression="nombre_archivo" />
</Columns>

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.

grid posts back when checkbox is checked

I have this grid set up.... it all works totally fine... except one issue...
<asp:GridView runat="server"
ID="grdFacetsAssigned"
AllowPaging="false"
AllowSorting="True"
DataKeyNames="lngSystemFacet"
OnSelectedIndexChanging="grdFacetsAssigned_SelectedIndexChanging"
CssClass="table_scroll"
AutoGenerateColumns="False" GridLines="None"
ShowHeader="false" Width="500px"
OnSelectedIndexChanged="grdFacetsAssigned_SelectedIndexChanged"
ShowFooter="false" PagerSettings-Visible="false"
DataSourceID="SM_spStateUpdateReport_FacetAssignList"
OnRowCreated="grdFacetsAssigned_RowCreated">
<RowStyle CssClass="table_row" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lbllngSystemFacetID" runat="server"
Text='<%# Eval("lngSystemFacetID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="strSystemSystemFacet" SortExpression="strSystemSystemFacet"
ItemStyle-Width="50%" />
<asp:TemplateField ItemStyle-Width="30%" ItemStyle-HorizontalAlign="Center"
SortExpression="bolAssigned">
<ItemTemplate>
<asp:CheckBox ID="chkFacetAssigned" runat="server"
OnClientClick="alert(this.checked);"
OnCheckedChanged="chkFacetAssigned_CheckedChanged"
AutoPostBack="True" Checked='<%# Eval("bolAssigned") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="intOrder"
HeaderText="Display Order" ItemStyle-Width="20%">
<ItemTemplate>
<asp:Label ID="lblAssignedFacetOrder" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "intOrder")%>'></asp:Label>
<asp:TextBox ID="txtAssignedFacetOrder" runat="server"
CssClass="gridview_input"
Text='<%#DataBinder.Eval(Container.DataItem, "intOrder")%>'
Visible="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle CssClass="table_selected_row" />
<AlternatingRowStyle CssClass="table_alternating_row" />
<EmptyDataRowStyle CssClass="table_empty" />
<EmptyDataTemplate>
No Data
</EmptyDataTemplate>
</asp:GridView>
When you click the chkFacetAssigned checkbox the appropriate event fires. The code works well from there. What happens though is when the checkbox is checked... if the row is not selected there are two postbacks that happen. The first postback is from the grid and the second postback is from the checkbox. Both postbacks cause the chkFacetAssigned_CheckedChanged event to be called- resulting in code running twice that should only run once. I should note that if the row is already selected (the row the checkbox is on) you do not see this extra postback. Someone please help.
There are no other event handlers registered or anything like this.
First line of your code. Remove the following.
OnSelectedIndexChanging="grdFacetsAssigned_SelectedIndexChanging"
Second Line
OnSelectedIndexChanged="grdFacetsAssigned_SelectedIndexChanged"
What I did for this was a workaround in the checkbox event handler...
if (Page.Request.Params["__EVENTTARGET"].IndexOf("chkFacetAssigned") < 1)
{
return;
}
This ensures that the event is ignored unless it is responding to a postback that was initiated by the checkbox and not the grid.

Categories