my stored procedure is returning either 1 or 0 depending on the value of another field, however, my checkboxfield in the gridview I've created to display the data returned from the stored proc, is crashing saying that the value set to the checkboxfield is a string, not boolean. How can I take the field returned as 1 or 0 and convert it to boolean so my checkbox can bind to this value for checking/unchecking?
aspx
<asp:TemplateField SortExpression="TragamonedaActiva" HeaderText="Trag. Activa">
<ItemTemplate>
<asp:CheckBox ID="CK2" runat="server" EnableViewState="true"
Checked='<%# Convert.ToBoolean(Eval("TragamonedaActiva")) %>'/>
</ItemTemplate>
</asp:TemplateField>
.cs
isChecked = ((CheckBox)gvReport.Rows[rowNo].FindControl("CK1")).Checked;
I belive you want something like this - this works for a DataGrid:
<asp:CheckBox ...
Checked='<%# Convert.ToBoolean( DataBinder.Eval(Container.DataItem, "is_checked"))%>'
/>
Create a template field with your checkbox in the datagrid.
// In your aspx page
<asp:CheckBox ID="yourCheckBox" runat="server" OnDataBinding="yourCheckBox_DataBinding" />
// In your codebehind .cs file
protected void yourCheckBox_DataBinding(object sender, System.EventArgs e)
{
CheckBox chk = (CheckBox)(sender);
chk.Checked = Convert.ToBoolean(Eval("YourFieldName"));
}
Related
I want grid view header template textbox value. I write code for getting value but it return null.
<asp:Button ID="btngetLocationDate" runat="server" Text="Get Filtered Data" OnClick="getTextBoxValue"></asp:Button>
<asp:TemplateField HeaderText="Mobile Number">
<HeaderTemplate>
Mobile Number:
<asp:TextBox ID="txtMobilenumber" runat="server" ></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblmobile" runat="server" Text='<%# Eval("Mobile Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And On the code behind.cs i write that code but it returns null
protected void getTextBoxValue(object sender, EventArgs e)
{
//TableCell cell1 = TrackerGrid.HeaderRow.Cells[0];
TextBox mobilenumber = (TrackerGrid.HeaderRow.FindControl("txtMobilenumber") as TextBox) ;
string mobile = mobilenumber.Text;
How can I resolve this ? please help me!
I've just had a similar problem by trying to get textbox text from the footer row. I'm not sure if this is the best way to get around this problem but this should work if FindControl() successfully assigns the textbox.
TextBox mobilenumber = (TrackerGrid.HeaderRow.FindControl("txtMobilenumber") as TextBox);
string mobile = Request.Form[mobilenumber.UniqueID];
EDIT: As VDWWD commented, Request.Form is a bad practice. But for my particular usage I couldn't find any other way to get the data from a textbox on button click due to my unorganized postbacks.
Is there someway to turn the row of a gridview into a hyperlink so that when a user opens it in a new tab for example, it goes to that link? Right now I am using a LinkButton and when the user opens it in a new tab, it doesn't know where to go.
I figured the .aspx code would look something like:
<asp:TemplateField>
<ItemTemplate>
<Hyperlink ID="hyperlink" runat="server" ForeColor="red" HtmlEncode="false" navigationURL="testUrl.aspx"
</ItemTemplate>
</asp:TemplateField>
The only thing is, our URLs are set up in the C# code behind as a query string, so I'm not sure how to pass that into the navigationURL section.
I'm guessing there's something I can do on the page_load with the query string to redirect to the page I need, but this is my first time working with query strings so I'm a little confused.
Thanks!
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#String.Format("~/controller.aspx?routeID1={0}&routeID2={1}", Eval("routeid1"), Eval("routeid2"))%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
routeid1 and routeid2 are passed as query strings to the controller of that page.
What I did recently is modified my class to have a readonly property that constructs the A tag for me. This way I have control over what gets displayed; just text or a link.
<ItemTemplate>
<asp:Label ID="ColumnItem_Title" runat="server" Text='<%# Bind("DownloadATag") %>'> </asp:Label>
</ItemTemplate>
The code behind just binds an instance of the class to the gridview. You can bind the gridview whenever, on load on postback event, etc.
Dim docs As DocViewList = GetViewList()
GridViewDocuments.DataSource = docs
GridViewDocuments.DataBind()
In the above code, the DocViewList, instantiated as docs, is a list of a class that has all the properties that are needed to fill my GridView, which is named GridViewDocuments here. Once you set the DataSource of your GridView, you can bind any of the source's properties to an item.
Something like:
<asp:LinkButton ID="LinkButton_Title" runat="server" target="_blank"
PostBackUrl='<%# Eval(Request.QueryString["title"]) %>'
or binding them from the RowCreated event:
protected void GridView_OnRowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.FindControl("LinkButton_Title") as LinkButton).PostBackUrl = Request.QueryString["title"]))
}
}
I am using DetailsView which its DefaultMode: insert, and I want to make its checkbox to be checked by default also user can change it to unchecked, but to bind checkbox we should use
Checked='<%# Bind("Cit_Visible") %>'
and this lets the default status of checkbox to be unchecked, so how can I solve this?
You can assign value to text property of checkbox if you want your check box selected at the time of data binding.
<asp:CheckBox ID="chl" runat="Server" Checked="true" Text="<%# Bind('Cit_Visible') %>" />
on code behind you can access text value to save it to in DB
CheckBox MyCheckbox = new CheckBox();
MyCheckbox = (CheckBox)DetailsView1.FindControl("chl");
Response.Write(MyCheckbox.Checked);
When using a DetailsView data control and you have checkbox values you may be starting with an asp:CheckBoxField which handles all the display modes for you. If you want to keep the checkbox binding but also set the default to checked perhaps for an insert you can do the following.
Convert the field to a TemplateField which can be done through the design view of visual studio or manually by replacing this type of block..
<asp:CheckBoxField DataField="Information" HeaderText="Information" SortExpression="Information" />
with a block of code like this
<asp:TemplateField HeaderText="Information" SortExpression="Information">
<EditItemTemplate>
<asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' />
</InsertItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkInformation" runat="server" Checked='<%# Bind("Information") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
Then to set the checkbox default value to be checked you can do this in the code-behind
Protected Sub dvInformation_PreRender(sender As Object, e As EventArgs) Handles dvInformation.PreRender
If CType(sender, DetailsView).CurrentMode = DetailsViewMode.Insert Then
Dim chk As Object = CType(sender, DetailsView).FindControl("chkInformation")
If chk IsNot Nothing AndAlso chk.GetType Is GetType(CheckBox) Then
CType(chk, CheckBox).Checked = True
End If
End If
End Sub
C# (Converted from VB
protected void dvInformation_PreRender(object sender, EventArgs e)
{
if (((DetailsView)sender).CurrentMode == DetailsViewMode.Insert) {
object chk = ((DetailsView)sender).FindControl("chkInformation");
if (chk != null && object.ReferenceEquals(chk.GetType(), typeof(CheckBox))) {
((CheckBox)chk).Checked = true;
}
}
}
This is obviously best when the supporting database value is a non-null bit field
Use TemplateField:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk1" runat="server" OnInit="chk1_Init" Checked='<%# Bind("Cit_Visible") %>' />
</ItemTemplate>
</asp:TemplateField>
Set the checkbox default value in the Init method:
protected void chk1_Init(object sender, EventArgs e)
{
((CheckBox)sender).Checked = true;
}
I have multiple textboxes and dropdown lists within my GridView. For one particular textbox I need trigger a server event which gets data from the database and fills it in other columns of the Grid. Is there a simple way to do it or a slightly complicated way as detailed here
I have no problems implementing the above method or thinking of a work around but then thought that there is Cell Lost Focus in a grid control surprises me a little. Am I missing something ? Any help on this would appreciated.
You can set AutoPostBack to true and handle it's TextChanged event.
<asp:GridView ID="GridView1" runat="server" EmptyDataText="It's Empty.">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName"
runat="server"
Text='<%#Eval("Name") %>'
AutoPostBack="true"
OnTextChanged="NameChanged" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</GridView>
in codebehind:
protected void NameChanged(Object sender, EventArgs e)
{
var txtName = (TextBox) sender;
var row = (GridViewRow) txtName.NamingContainer;
// you could find other controls in this GridViewRow via
// row.FindControl("ControlID") in case of a TemplateField or
// row.Cells[0].Text (0 = index of column) in case of a BoundField
}
I have a repeater for showing my data . this repeater showing 2 field that one of feild is checkBox Control and other is a lable.
NOW , how can I understand text of lable when the checkBox is Checked?
I want to see text of lable in evry row that the CheckBoxes is checksd.
how do I do?
I use LINQtoSQL for get and set data from database
On postback, you need to loop through every row of your repeater, and grab out the checkbox control. Then you can access it's .Checked and .Text properties. If it's .Checked, then add it to a list or array. I can elaborate if needed..
Page...
<asp:CheckBox ID="chkBoxID" runat="server" OnCommand="doSomething_Checked" CommandArgument="<%# Some Binding Information%>"
CommandName="NameForArgument">
</asp:CheckBox>
Code Behind...
protected void doSomething_Checked(object sender, CommandEventArgs e) {
CheckBox ctrl = (CheckBox)sender;
RepeaterItem rpItem = ctrl.NamingContainer as RepeaterItem;
if (rpItem != null) {
CheckBox chkBox = (LinkButton)rpItem.FindControl("chkBoxID");
chkBox.DoSomethingHere...
}
}
<asp:Repeater ID="rptX" runat="server">
<ItemTemplate>
<asp:Label ID="lblX" runat="server" Visible='<%# Eval("IsChecked") %>' />
<asp:CheckBox ID="chkX" runat="server" Checked='<%# Eval("IsChecked") %>' />
</ItemTemplate>
</asp:Repeater>
And code behind when you assign your data
rptX.DataSource = SomeIEnumerableFromLinq; // which has a bool field called IsChecked
rptX.DataBind();