I am trying to implement validations on textbox like if null then display a message to fill the empty field and to check the length of the text entered. I have written the code for it inside TextBox_TextChanged event but it's not working. The event is not getting fired and the user can is able to signup without a username, that is my problem at the moment. Do I have to trigger the event manually? Here is a glimpse of what I am doing:
protected void FirstN_TextBox_TextChanged(object sender, EventArgs e)
{
String firstNameEntered = FirstN_TextBox.Text;
if (firstNameEntered != null)
{
if (firstNameEntered.Length <= 20)
{
MessageBox.Show("Inside text box");
}
else
{
}
}
else
{
FirstN_TextBox.Focus();
MessageBox.Show("Please fill the marked field");
}
change it like this:
From:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox>
To:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged" AutoPostBack ="true"></asp:TextBox>
Good day. I have a trouble with pass data between webform and UserControl.
I have the webform webform1.aspx. And i have a component GridView1. I also have UserControl - grdControl.ascx.
In webform1.aspx i mention UserControl:
<uc1:grdcontrol runat="server" id="grdControl" />
Also webform1.aspx.cs contains an event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
grdControl.SelectedValue = GridView1.SelectedValue.ToString();
}
In turn UserControl grdControl.ascx.cs contains the following code:
private string _selectedValue;
public string SelectedValue
{
get { return _selectedValue; }
set { _selectedValue = value; }
}
In grdControl.ascx i use label and i try to get value SelectedValue and use it as text for Label1.
<asp:Label ID="Label2" runat="server" Text='<%# SelectedValue%>' ></asp:Label>
But is don't work correctly. In a web page i see nothing.
Setting the value of the label in the PreRender event of the code behind should fix it.
I'm thinking that GridView1_SelectedIndexChanged will get called after the Page_Load event of the inner grid control that's why the value is blank because you are trying to set it in the page itself and that happens in the Pre_init function of the inner control. I don't think SelectedValue would be set at that point.
I have a dataGridView, which is data bound to some generic List<T> (while T is some custom class with properties).
The problem is that one of the property is type of integer, and it represents minutes.
After binding List to dataGridView, I want that column shows hours, instead of minutes by default.
How to change some column`s behaviour, to use some math over it to show a bit different values?
Do I have to do this in the DataGridView.CellFormating event?
You have two options, the first one is to manipulate your Generic List<T> first, that should be faster than using the second option, iterating through your list on each RowDataBound Event.
Using RowDataBound Event
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int minutes = int.Parse(e.Row.Cells[YourColumnIndex].Text);
decimal hours = minutes / 60;
e.Row.Cells[YourColumnIndex].Text = hours.ToString();
}
}
Using Evel Expression
ASPX Page
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<%# ConvertToHours(Eval("Minutes"))%>
</ItemTemplate>
</asp:TemplateField>
Code Behind
private string ConvertToHours(object objMin)
{
if (Convert.ToInt32(objMin) == 1)
{
return (int.Parse(objMin) / 60).ToString();
}
else
{
return "0";
}
}
Another approach. - do-it-all in single shot.
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<asp:Label ID="lblTime" runat="server" Text='<%# Convert.ToInt32(Eval("Time")) Convert.ToInt32("60")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Update: As the Question updated, Then for Windows Forms application you should use DataGridView.CellFormatting Event
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// If the column is the Time column, check the
// value.
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Time")
{
if (e.Value != null)
{
//Your implementation.
}
}
}
Question pretty much says it all. On my aspx page I have a GridView and under Columns I have a bunch of BoundFields, one of which is a TemplateField
<asp:TemplateField HeaderText = "Status">
<ItemTemplate>
<asp:HyperLink ID = "HyperLink1" runat = "server" Target = "_blank"
NavigateUrl = '<%# Eval("URL") %>'
Text = '<%#Eval("Status") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Now, I want this Hyperlink to map to a different image, depending on what the text is evaluated to. For example, 'Success' displays a big ol' smiley face instead, 'Failed' displays a frowney face, and so on. How can I achieve this?
Thanks for looking.
You can put an image in the hyperlink like
<img src='/images/status/<%#Eval("Status") %>.jpg' />
and just make a different image for each status by name. Otherwise you'll probably have to do something on the DataBind event.
Try this
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink1 = e.Row.FindControl("HyperLink1");
if(SomeText == "Success")
HyperLink1.NavigateUrl = "Url to Smiley";
else
HyperLink1.NavigateUrl = "Url to Frowney";
}
}
HyperLink HyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
switch (HyperLink1.Text)
{
case "Completed":
HyperLink1.ImageUrl = "Images\\Success.png";
HyperLink1.ToolTip = "Completed";
etc
The ToolTip property maps to the alternate text for the image.
Thanks to codingbiz for getting me started.
If you are trying to set the ImageUrl property I suggest using the RowDataBound event. The handler method could would look something like:
protected void questionsGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
DataSourceDataType row;
HyperLink hyperLink1;
if (e.Row.RowType == DataControlRowType.DataRow & e.Row.DataItem is DataSourceDataType)
{
row = (DataSourceDataType)e.Row.DataItem;
hyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
hyperLink1.ImageUrl = (row.IsSuccess) ? "~/images/success.png" : "~/images/failure.png";
}
}
Another trick I have used is altering the data object you are binding to to have a property which indicates the URL to use:
partial class DataSourceDataType
{
public string SuccessImgURL
{
get
{
return (IsSuccess) ? "~/images/success.png" : "~/images/failure.png";
}
}
}
Then you bind to that property.
Note: IsSuccess would need to be replaced with your own field name or boolean condition.
I often use this with LINQ to SQL objects, so adding properties can be done in a separate file using partial classes. This way you do not have to worry about the LINQ to SQL tools removing your additions.
in my .aspx I'm looking to add in an If statement based on a value coming from the bind. I have tried the following:
<% if(bool.Parse(Eval("IsLinkable") as string)){ %>
monkeys!!!!!!
(please be aware there will be no monkeys,
this is only for humour purposes)
<%} %>
IsLinkable is a bool coming from the Binder. I get the following error:
InvalidOperationException
Databinding methods such as Eval(), XPath(), and Bind() can only
be used in the context of a databound control.
You need to add your logic to the ItemDataBound event of ListView. In the aspx you cannot have an if-statement in the context of a DataBinder: <%# if() %> doesn't work.
Have a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx
The event will be raised for each item that will be bound to your ListView and therefore the context in the event is related to the item.
Example, see if you can adjust it to your situation:
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
bool linkable = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable");
if (linkable)
monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
}
}
I'm pretty sure you can do something like the following
(Note I don't have a compiler handy to test the exact syntax)
text = '<%# string.Format("{0}", (bool)Eval("IsLinkable") ? "Monkeys!" : string.Empty) %>'
Yes this is c# and your using vb.net, so you'll need to use vb syntax for a ternary operator.
Edit - was able to throw into into a simple data bind situation, worked like a charm.
You can use asp:PlaceHolder and in Visible can put eval. Like as below
<asp:PlaceHolder ID="plc" runat="server" Visible='<%# Eval("IsLinkable")%>'>
monkeys!!!!!!
(please be aware there will be no monkeys, this is only for humour purposes)
</asp:PlaceHolder>
OMG this took entirely too long to figure out...
<asp:PlaceHolder runat="server" Visible='<%# Eval("formula.type").ToString()=="0" %>'>
Content
</asp:PlaceHolder>
formula.type is a linked table's int column. Thanks for the other contributions to get my resolution.
If you are having issues getting e.Item.DataItem in Bazzz's answer try
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
using (ListViewDataItem listViewDataItem = (ListViewDataItem) e.Item)
{
if (listViewDataItem != null)
{
Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
bool linkable = (bool)DataBinder.Eval(listViewDataItem , "IsLinkable");
if (linkable)
monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
}
}
}
I know it is a bit late in the day for this answer but for what it is worth here is my solution to the problem:
<%# (bool)Eval("IsLinkable") ? "monkeys!!!!!!" : "" %>
You can create a method to evaluate the value and return the value you want.
<%# IsLinkableABool( Eval("IsLinkable") ) %>
On the code behind you can create the method as follow
protected String IsLinkableABool(String isLinkable)
{
if (isLinkable == Boolean.TrueString)
{
return "monkeys!!!!!! (please be aware...";
}
else
{
return String.Empty;
}
}
Whenever I've needed to handle conditions within a databound control, I use the OnItemDataBound event.
So you could do:
protected void DataBound_ItemDataBoundEvent() {
bool IsLinkable = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable");
if(IsLinkable) {
//do stuff
}
}
We would need to see the rest of your code, but the error message is giving me a bit of a hint. You can ONLY use Eval when you are inside of a data-bound control. SOmething such as a repeater, datagrid, etc.
If you are outside of a data bound control, you could load the value into a variable on the code-behind and make it public. Then you could use it on the ASPX for conditional processing.
For FormView Control refer to this link.
Here is the sample code. My aspx page FormView Control look like below:
<asp:FormView ID="fv" runat="server" Height="16px" Width="832px"
CellPadding="4" ForeColor="#333333" ondatabound="fv_DataBound">
<ItemTemplate>
<table>
<tr>
<td align="left" colspan="2" style="color:Blue;">
<asp:Label ID="lblPYN" runat="server" Text='<%# Eval("PreviousDegreeYN") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
I am checking the value for <%# eval("PreviousDegreeYN") %>
If my eval("PreviousDegreeYN") == True, I want to display Yes in my label "lblPYN"
protected void fv_DataBound(object sender, EventArgs e)
{
FormViewRow row = fv.Row;
//Declaring Variable lblPYN
Label lblPYN;
lblPYN = (Label)row.FindControl("lblPYN");
if (lblPYN.Text == "True")
{
lblPYN.ForeColor = Color.Blue;
lblPYN.Text = "Yes";
}
else
{
lblPYN.ForeColor = Color.Blue;
lblPYN.Text = "No";
}
}
Putting condition aspx page is not a good idea.also messy.
U can do using ternary operator.But I suggest u to use rowdatabound events of grid view.
step 1-go to grid view properties.Click on lighting button to list all event.
Step 2-give a name on rowdatabound and double click
protected void onrow(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell statusCell = e.Row.Cells[8];//Means column 9
if (statusCell.Text == "0")
{
statusCell.Text = "No Doc uploaded";
}
else if (statusCell.Text == "1")
{
statusCell.Text = "Pending";
}
else if (statusCell.Text == "2")
{
statusCell.Text = "Verified";
}
}
}