GridView Template Issue with Javascript - c#

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>

Related

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>

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.

Customizing asp:HyperLinkField

I am using the standard GridView.
I have this part working so far.
<asp:HyperLinkField ShowHeader="true" DataTextField="id" DataNavigateUrlFields="id"
DataNavigateUrlFormatString="edit.aspx?id={0}"
DataTextFormatString="Edit" />
However each page needs a 'number' and a 'userid' parameter.
I cannot figure out how to add these parameters to the above HyperLinkField.
<asp:HyperLinkField ShowHeader="true" DataTextField="id"
DataNavigateUrlFields="id"DataNavigateUrlFormatString="edit.aspx?id={0}
&number=Request.QueryString["number"]&
userid=Request.QueryString["userid"]" DataTextFormatString="Edit" />
Can someone tell me what I am missing to be able to add this custom URL to my HyperLinkField?
Try using a templatefield, something like (untested):
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<a href='<%# "edit.aspx?id="+Eval("id") + "&number=" +Request.QueryString["number"]+"&userid=" +Request.QueryString["userid"] %>'>Edit</a>
</ItemTemplate>
</asp:TemplateField>

How to define number of rows of GridView at runtime in code behind using C#

I need to develop such a program in which the GridView's rows should be decided at run time.
i.e. I have a table in database called dealer capacity.
Dealer_ID Capacity
D0001 5
Now when the Dealer D00001 is selected from combo box the number of rows in grid view should be 5. I want to use the template field also.
My code for GridView is:
<asp:GridView ID="grdlicence" runat="server" DataKeyNames="Version_id" GridLines="None" BorderStyle="Solid" AutoGenerateColumns="false" AllowSorting="true"
CssClass="mGrid table"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" >
<Columns>
<asp:BoundField DataField="Version_name" ItemStyle-CssClass="uppercase" ItemStyle-Width="150px" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Version_id" Visible="false" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<center><asp:TextBox ID="txtprice" CssClass="alignments TextStyle" MaxLength="5" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Licence Id">
<ItemTemplate>
<center><asp:TextBox ID="txtlicenceid" CssClass="alignments uppercase" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase Date">
<ItemTemplate>
<center><asp:TextBox ID="txtpurchasedate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiry Date">
<ItemTemplate>
<center><asp:TextBox ID="txtexpirydate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Upload File">
<ItemTemplate>
<center><asp:FileUpload ID="fileUpload" runat="server" /></center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You need to define PageSize for your GridView and remember to set AllowPaging to true for the GridView
GridView.PageSize Property
Gets or sets the number of records to display on a page in a GridView
control.
The default is 10.
You may see this article: GridView Paging Sample in ASP.NET
You can use the linq Take() and pass the number as parameter.
Updated according to the comment, use following code.
grdlicence.DataSourse= ds.Take(5);
grdlicence.DataBind();

asp:Button is not calling server-side function

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.

Categories