I'm having trouble figuring out how to control the visibility of an image in a gridview based on a session variable.
<asp:Image runat="server" ID="imgImportedData" Width="20px" Height="20px" ImageUrl="~/images/warning.png" CausesValidation="false" />
I tried using Visible='<%# mySessionVariable %>' but I got a message saying mySessionVariable was unavailable. I think this is because it's in a grid because I am using this variable in the code behind for another part of the page outside of the gridview without any problems.
EDIT: I just realized this in a Repeater control and not a GridView.
I tried both of these and still get The name 'MySession' does not exist in the current context
Visible='<%# (bool)MySession.IsImportedData == "true" ? true : false %>'
Visible='<%# MySession.IsImportedData == "true" ? true : false %>'
<%# is a DataBinding ASP server tag. What happens when you change <%# to <%=?
If that doesn't work, I would suggest setting the column's visibility in a RowDataBound event, like so:
MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image imgImportedData = (Image) e.Row.FindControl("imgImportedData");
// Assuming that mySessionVariable isn't already a bool, which it really should be.
imgImportedData.Visible = bool.Parse(mySessionVariable);
}
}
Try this:
<asp:Image runat="server" ID="imgImportedData"
Visible='<%# Session["mySessionVariable"] == "foo" ? true : false %>' />
I got this to work. Thank you for everyone's help. I found an example on this page that helped.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx
protected void rptAlternateNames_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem != null)
{
Image imageImportedData = ((Image)e.Item.FindControl("imgImportedData"));
if (MySession.IsImportedData)
{
imageImportedData.Visible = true;
}
}
}
}
Related
I have a simple checkBox in Editable gridView :
<asp:TemplateField HeaderText="Editable">
<ItemTemplate>
<asp:Label runat="server" Text="<%# Item.IsEditable %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBoxEditable " runat="server" Text="Editable"></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateField>
When I click on edit button in the row, I would like that checkBox is already checked if value is true. (IsEditable is a boolean)
Text Field is easy because I have a BindItem on Text property in EditItemTemplate. But it's not the same for checkBox or dropdownlist
GridView
I use a UpdateItem Method to update my data in database. I tried a small condition to check my checkBox but it does'nt work.
public void GridViewRisquesAggravants_UpdateItem(IndexViewModel item)
{
try
{
if (ModelState.IsValid)
{
CheckBox chbEdit = (CheckBox)GridView.Rows[this.GridView.EditIndex].FindControl("CheckBoxEditable")
if (item.IsEditable)
chbEdit.Checked = true;
new TypeService().Update(new Type
{
IsEditable = item.IsEditable,
});
this.GridView.DataBind();
}
}
catch
{
throw;
}
}
It makes sense because I am not in the right function to declare this. But I just have 3 methods in my webform.
SelectMethod="GridView_GetData"
UpdateMethod="GridView_UpdateItem"
DeleteMethod="GridView_DeleteItem"
Where can I do this?
(And I have the same problem with datas on dropdownList. I don't know where I recover current value during editing)
Thanks in advance
(Sorry I am beginner about webforms and my english is not perfect)
Evy
use the following code instead for checkbox declaration in edit template
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean("true") %>' />
CheckBox chbx = GridView1.HeaderRow.FindControl("CheckBoxEditable") as CheckBox;
if (chbx != null && chbx.Checked)
{
//code here
}
else
{
//else condtion
}
hope this helps
Where can I do this?
Try it in RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chbEdit = (CheckBox)e.Row.FindControl("CheckBoxEditable");
string value = ((Label)e.Row.FindControl("lblID")).Text;
if (value=="True")
chbEdit.Checked = true;
else
chbEdit.Checked = false;
}
}
Note: Don't forget to add OnRowDataBound in GrindView <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
I added the property Checked=<%# BindItem.IsEditable %> on CheckBox Control and it works perfectly.
I'am trying to hide/show the edit/save button in a ASP.NET Template.
so i want the edit button too show when no row is selected, and then hide it on click and then make the save button visable instead.
How do I access and update the attribute?
The solution ive tried just gives me "Null"
what i have:
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandName="Edit" ImageUrl="loginstyling/images/Edit.png"/>
<asp:ImageButton ID="ImageButtonUpdate" runat="server" CommandName="Update" ImageUrl="loginstyling/images/Save.png" OnClick="ImageButtonUpdate_Click" Visible="true"/>
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="loginstyling/images/Remove.png" visible="false" />
</ItemTemplate>
what ive tried behind:
protected void ImageButtonUpdate_Click(object sender, ImageClickEventArgs e)
{
ImageButton test = (ImageButton)GridView1.FindControl("ImageButtonUpdate");
test.Attributes.Add("Visible", "False");
}
Try if this works:
test.Visible = false;
Try using RowDataBoundEvent for GridView, if it works:
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton test = (ImageButton)e.Row.FindControl("ImageButtonUpdate");
test.Visible = false;
}
}
Are you sure that the .FindControl("ImageButtonUpdate");returns a valid object?
Since it returns null check this other post FindControl() return null , it seems to be the same problem you have encountered.
I have a Repeater which loads some data from a SQL database
<asp:Repeater ID="Repeater" runat="server" OnItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:Label ID="QuestionLabel" runat="server" Text=""></asp:Label>
<asp:TextBox ID="AnswerLabel" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="AnswerSubmit" runat="server" Text="Insert"/>
In code behind, i assign to get the questions on another button click to load and bind the Repeater. In ItemDataBound i find the controls and assign the questions to the label.
How should i get the answers that the user enters and store the ID of the question with the answer they enter in a button click event?
At first, I tried this in the button click event
foreach (RepeaterItem item in Repeater.Items)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
but it couldnt find the QuestionLabel. Looking at the page source i believe thats due to the label having a different value (QuestionLabel_0, QuestionLabel_1 etc), so struggling to find a way to approach this?
Edit
protected void QuestionButton_Click(object sender, EventArgs e)
{
Repeater.DataSource = QuestionsByID(ID);
Repeater.DataBind();
}
You see
foreach (RepeaterItem item in Repeater.Items)
{
if (item.ItemType == ListItemType.Item
|| item.ItemType == ListItemType.AlternatingItem)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
}
I want to set visible property of button control from design side using server tag <%# %>. I can do it from code behind in page load method by checking query string mode value like below:
if (!IsPostBack)
{
---
if (Request.QueryString["mode"] != null && Request.QueryString["mode"] == "1")
{
btndelete.Visible = false;
----
}
else if (Request.QueryString["mode"] != null && Request.QueryString["mode"] == "2")
{
btndelete.Visible = true;
----
}
}
Now instead of writing code from code behind i want to check query string mode value from server tag and returned result will be set to Visible property of button.
I have tried this way but No Luck!
<asp:Button ID="btndelete" CausesValidation="false" Text="<%$Resources:General,Delete%>" OnClick="btndelete_Click"
runat="server" CssClass="btnstyle" OnClientClick="showConfirm(this,'mdlAttendanceReportCriteriaDelete'); return false;"
Visible='<%#if(Request.QueryString["mode"].ToString() == "1"){Convert.ToBoolean("false")}else{Convert.ToBoolean("true")} %>'/>
<asp:Button ID="btndelete" CausesValidation="false" Text="<%$Resources:General,Delete%>" OnClick="btndelete_Click"
runat="server" CssClass="btnstyle" OnClientClick="showConfirm(this,'mdlAttendanceReportCriteriaDelete'); return false;"
Visible='<%#(Request.QueryString["mode"].ToString() == "1")?Convert.ToBoolean("false"):Convert.ToBoolean("true") %>'/>
<asp:Button ID="btndelete" CausesValidation="false" Text="<%$Resources:General,Delete%>" OnClick="btndelete_Click"
runat="server" CssClass="btnstyle" OnClientClick="showConfirm(this,'mdlAttendanceReportCriteriaDelete'); return false;"
Visible='<%#!(Request.QueryString["mode"].ToString() == "1") %>'/>
It run without any parser error but there is no effect at all. Can anybody tell me how to achieve this functionality ?! Thanks in advance.
Call DataBind() method in Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
then use this syntax to set the Visible property in aspx code:
Visible='<%# Request.QueryString["mode"] == "2" %>'
What about using
Visible='<% Request.QueryString.Get("mode") == "1"? "true": "false"%>'
or
Visible='<% if (Request.QueryString.Get("mode") == "1" ) "true" else "false" %>'
Visible='<%# Request.QueryString.Get("mode").Equals("1") ? Convert.ToBoolean("False") : Convert.ToBoolean("True") %>'
This is how I navigate to myPage.aspx ,
Show Each
Show All
And I have a gridview in myPage.aspx
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="ColumnOne" Visible="true"/>
<asp:BoundField HeaderText="ColumnTwo" Visible="true"/>
</Columns>
</asp:GridView>
What I want to do is , if Query String is equal to all(~/myPage.aspx?show=all) , I want to set GridView1's Column2's visible to true , else , set visible to false .
How can I do it ?
You can use gridview pre-render method to set this...
protected void GridView_PreRender(object sender, EventArgs e)
{
if(Reqest.QueryString["Id"]=="all"&& Reqest.QueryString["Id"]!=null)
{
GridViewId.Columns[1].Visible = true;
}
else
GridViewId.Columns[1].Visible = false;
}
you can use gridview column index to hide the particular column
Code could be
if(Request.QueryString.Get("show")=="all")
GridView1.Columns[1].Visible=true;
else
GridView1.Columns[1].Visible=false;
More detail
GridView Hide Column by code
Edit 3
Settings in ASPX/ASCX can not be done directly.
<%= %> outputs directly to the response stream, and the asp markup is not part of the response stream. Its a mistake to assume the <%= %> operators are performing any kind of preprocessing on the asp markup.
More explanation
Why will <%= %> expressions as property values on a server-controls lead to a compile errors?
Edit 1
I think yes
<asp:BoundField HeaderText="ColumnTwo"
Visible='<% if (Request.QueryString.Get("all") == "all" ) "true" else "false" %>'/>
You will have to check for the syntex
Edit 2
Try this
Visible='<% Request.QueryString.Get("all") == "all"? "true": "false"%>'
Dear try to use RowDataBound event of Grid View like
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//here apply your condition
if(Request.QueryString["name"] == "all")
e.Row.Cells[<index_of_cell>].Visible = true;
else
e.Row.Cells[<index_of_cell>].Visible = false;
}
}
Try something like that.
Hope it works for you.