Hi there, I hope your appreciated help.
First of all I must say that I am a newbie when it comes to net language.
This is my problem.
How to disabled the asp image button "ImageButton4" when today's date is greater than the registration date in my database ?.
For example the record with date registration '2012-04-14' should not display the asp image button "ImageButton4".
If you have link for similar task, please give it me.
Can you help me? thank you in advance.
Your help would be very appreciated
Thanks for your time and hints.
This is my code:
protected void ImageButton1_Click(object sender, EventArgs e)
{
ImageButton myButton = (ImageButton)sender;
string ID = myButton.CommandArgument.ToString();
Response.Redirect("http://localhost/WebApplication1/default.asp?id=" + ID + "&cks=" + Request.Cookies["MyCookie"].Value + "");
}
<ItemTemplate>
<asp:ImageButton ID="ImageButton4" CommandName="Redirect"
CommandArgument='<%#Eval("ID")%>' runat="server"
ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
OnClientClick="return confirm('Confirm?.');"
onclick="ImageButton1_Click" />
</ItemTemplate>
[C#] Comparing dates in GridView
6 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Abhimanyu K VatsaRank #14 · 51.6K pointsPosted Apr 16, 2012, 11:14 AM EDT
protected void Page_Load(object sender, EventArgs e) { DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0); DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0); int result = DateTime.Compare(date1, date2); if (result < 0) ImageButton4.Enable = false; else //do the rest part }Chevy Mark Sunderland115 pointsPosted Apr 16, 2012, 11:32 AM EDT
ImageButton4 does not exist in current context
public void Page_Load (Object sender, EventArgs e)
{
OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
myConnectionString.Open();
myConnectionString.Dispose();
myConnectionString.Close();
lblMessage.Text = "";
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
if (result < 0) {
ImageButton4.Enable = false;
}
}
<ItemTemplate>
<asp:ImageButton ID="ImageButton4" CommandName="Redirect"
CommandArgument='<%#Eval("ID")%>' runat="server"
ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
OnClientClick="return confirm('Confirm?.');"
onclick="ImageButton1_Click" />
</ItemTemplate>
SenthilkumarRank #387 · 6.5K pointsPosted Apr 16, 2012, 8:23 PM EDT
CommandArgument='<%#Eval("ID")%>' runat="server" ImageUrl="/Images/updateDB.gif" ToolTip="Update DB" OnClientClick="return confirm('Confirm?.');" onclick="ImageButton1_Click" />Hope this will help you.
Chevy Mark Sunderland115 pointsPosted Apr 17, 2012, 1:29 AM EDT
thank you for your help.
I try your suggestion but I have this error, why? I hope your help.
Compiler Error Message: CS1061: 'System.Web.UI.WebControls.GridViewRow' does not contain a definition for 'Findcontrol' and no extension method 'Findcontrol' accepting a first argument of type 'System.Web.UI.WebControls.GridViewRow' could be found (are you missing a using directive or an assembly reference?)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lblDate = e.Row.Findcontrol("lblDate"); ImageButton ImageButton4 = e.Row.FindControl("ImageButton4"); DateTime dtDB = lblDate.Text; Datetime dtToday = DateTime.Now; if (dtToday > dtDB) { ImageButton4.Enabled = false; } } } <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ID" AllowSorting="True" AllowPaging="True" PageSize="15" AutoGenerateColumns="false" CssClass="first" OnRowUpdated="GridView1_RowUpdated" OnRowDataBound="GridView1_RowDataBound">CommandArgument='<%#Eval("ID")%>' runat="server" ImageUrl="/Images/updateDB.gif" ToolTip="Update DB" OnClientClick="return confirm('Confirm?.');" onclick="ImageButton1_Click" />SenthilkumarRank #387 · 6.5K pointsPosted Apr 17, 2012, 1:36 AM EDT
You need to bind the data base date into lblDate control.
<ItemTemplate>
<asp:Label id="lblDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"datecolumnfromDB") %>' />
</ItemTemplate>
And also when you find the control,
Chevy Mark Sunderland115 pointsPosted Apr 17, 2012, 2:16 AM EDT
thank you for your help.
I edit my net code with your suggestions but I have the new error.
I hope your help.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 117: if (dtToday > dtDB)
Line 118: {
Line 119: ImageButton4.Enabled = false;
Line 120: }
Line 121: }
<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1"
DataKeyNames="ID"
AllowSorting="True"
AllowPaging="True"
PageSize="15"
AutoGenerateColumns="false"
CssClass="first"
OnRowUpdated="GridView1_RowUpdated"
OnRowDataBound="GridView1_RowDataBound">
<asp:TemplateField HeaderText="myDate" SortExpression="myDate">
<ItemTemplate>
<asp:Panel ID="Panel2" Runat="Server">
<asp:Label ID="Label2" Text='<%# Eval("myDate", "{0:dd/MM/yyyy}") %>' Runat="Server"/>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton4" CommandName="Redirect"
CommandArgument='<%#Eval("ID")%>' runat="server"
ImageUrl="/Images/updateDB.gif" ToolTip="Update DB"
OnClientClick="return confirm('Confirm?.');"
onclick="ImageButton1_Click" />
</ItemTemplate>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label Label2 = (Label)e.Row.FindControl("Label2");
ImageButton ImageButton4 = (ImageButton)e.Row.FindControl("ImageButton4");
DateTime dtDB = DateTime.ParseExact(Label2.Text, "dd/MM/yyyy", null);
DateTime dtToday = DateTime.Now;
if (dtToday > dtDB)
{
ImageButton4.Enabled = false;
}
}
}