ASP.Net NPE inside hidden parent container if not asp control - asp.net

Going back from JSF to ASP.Net I have a problem in following markup:
I get a null pointer exceptin on this.value within the panel component. I even called DataBind() on the panel itself.
<asp:Panel id="pnl" runat="server" visible="<%# this.value != null%>">
<%= this.value.Foo %>
</asp:Panel>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
if (this.value == null)
return;
this.pnl.DataBind();
}
Must i use an asp control always?

You probably have mistakes here :
You should do inside : <%# this.value.Foo %>
Capital letter : Visible {capital}).

The code inside the panel will still be executed even if the panel is not visible. It will not be rendered into the result but it will be executed. I would recommend that you use control properties and set your values via code behind on controls that are not repeating anything
<asp:Panel id="pnl" runat="server">
<asp:Literal ID="literal" runat="server" />
</asp:Panel>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
if(this.value != null)
{
pnl.Visible = true;
literal.Text = this.value.Foo;
}
else
{
pnl.Visible = false;
}
}

Related

How to hide a control inside user control after click gridview button

This is the problem that bothers me for the past few days
I have a page with couple user controls.
On that page, there is a grid. If a user click on a particular row, a control (li) on the user control is shown or hidden based on the condition.
What makes things complicate is the user control and grid grid are all inside the update panel, but another user control is outside updaet panel
In the rowcommand event, I assigned a Session variable, dontshow. Based on the criteria, dontshow variable will be assigned to true or false. Then the control on the user control will be shown or hidden based on dontshow condition
So far, I am encountering the problem.
let's say (li) control was shown, but after the user click gridview, dontshow is set to true. (li) control should be hidden. No it does not. I have to click other button to do postback to make it happen
The same is true for the usercontrol outside the update panel. The logic is similar
Over the past few days, I have found out that I can't set visible property of controls in user control at page_load because that happens after rowcommand event. I moved the code to pre_render event
I even tried to use different control such as placeholder, panel, etc. That still does not work
I also trace the code. The pre_render event comes after row command. dontshow variable is false. The (li) control visible is set to false. However, after rendering, I can still see the control
What should I do?
Below are the snapshot of the codes
Thank you for the help
main.aspx
.....
<%# Register Src="../usercontrol1.ascx" TagPrefix="uctest1" TagName="ctrl1" %>
<%# Register Src="../usercontrol2.ascx" TagPrefix="uctest2" TagName="ctrl2" %>
...
<ctrl1:uctest1runat="server" ID="test1" />
<asp:UpdatePanel ID="updatepantest1" runat="server" UpdateMode="Always">
<ContentTemplate>
<uctest2:ctrl2runat="server" UpdateMode="Conditional" ID="test2" />
....
<asp:GridView ...>
//grid where rowcommand was executed
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
main.aspx code behind
protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "something")
{
.....
if (x = 1)
session["dontshow"] = true
else
session["dontshow"] = false
}
}
user control aspx (usercontrol1)
....
<ul>
<li id="li1" runat="server">
<asp:label>this is test</asp:label>
</li>
<li >
<asp:label>this is test</asp:label>
</li>
<li >
<asp:label>this is test</asp:label>
</li>
</ul>
....
user control code behind (usercontrol1)
protected void Page_PreRender(object sender, EventArgs e)
{
bool dontshow = false;
if (Session["dontshow"] != null)
{
dontshow = (bool)Session["dontshow"]
}
if (dontshow)
li1.visible = false
else
li1.visible = true
}
user control aspx (usercontrol2)
....
<asp:PlaceHolder ID="placeholder1" runat="server">
<asp:label ID="label1" runat="server"></asp:label>
</asp:PlaceHolder>
....
user control code behind (usercontrol2)
protected void Page_PreRender(object sender, EventArgs e)
{
bool dontshow = false;
if (Session["dontshow"] != null)
{
dontshow = (bool)Session["dontshow"]
}
if (dontshow)
{
label1.visible =false;
}
else
{
label1.visible =true;
label1.Text ="this is test";
}
}
You could do it in your Row command like this
protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "something")
{
.....
var viewRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
HtmlGenericControl li1 = (HtmlGenericControl)viewRow.FindControl("li1");
if (x = 1)
{
session["dontshow"] = true;
li1.Visible = true;
}
else
{
session["dontshow"] = false;
li1.Visible = false;
}
}
}
Or, if you want to hide the control during data bound you could do it like this
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HtmlGenericControl li1 = (HtmlGenericControl)e.Row.FindControl("li1");
if (dontshow)
li1.visible = false;
else
li1.visible = true;
}
}
Hope this helps :-)

Repeater with Buttons doesn't fire OnItemCommand even when DataBind doesn't happen when IsPostBack is true

I've got an image button in a Repeater, which is in a User Control, and I can't get the OnItemCommand event to fire when I click it. It always gives the error: "... Invalid postback or callback argument. Event validation is enabled using ..."
When I change the image button to a link button, it doesn't give me the error, but it still doesn't fire the OnItemCommand function.
I found some other relevant answers (such as How to process events from Buttons inside Repeaters? And what's this EnableEventValidation thing?) but they all say, "Make sure you're data-binding your repeater inside of a if(!Page.IsPostBack) block." I have done that, but it didn't make a difference.
Here's the markup for the repeater:
<asp:Repeater ID="rptExpenses" OnItemDataBound="rptExpenses_ItemDataBound" OnItemCommand="Button_Command" runat="server" >
<ItemTemplate>
<asp:ImageButton ID="ibDelete" ImageUrl="~/Images/delete.png" CommandName="Delete" runat="server" />
</ItemTemplate>
</asp:Repeater>
Here's some exerpts from the code-behind
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
rptExpenses.DataSource = ExpenseIds;
rptExpenses.DataBind();
}
}
protected void rptExpenses_ItemDataBound(object sender, RepeaterItemEventArgs e) {
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) {
var ibDelete = (ImageButton)e.Item.FindControl("ibDelete");
ibDelete.CommandArgument = e.Item.DataItem.ToString();
}
}
protected void Button_Command(object sender, EventArgs e) {
var btn = (IButtonControl)sender;
switch (btn.CommandName) {
case "Delete":
//delete it
break;
}
}
Try using this code instead:
<asp:Repeater ID="rptExpenses" runat="server">
<ItemTemplate>
<asp:ImageButton ID="ibRemove" ImageUrl="~/Images/delete.png" runat="server"
CommandName="Remove"
CommandArgument='<%# Container.DataItem %>'
OnCommand="ibRemove_Click"/>
</ItemTemplate>
</asp:Repeater>
And this in the code behind:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
rptExpenses.DataSource = ExpenseIds;
rptExpenses.DataBind();
}
}
protected void ibRemove_Click(object sender, CommandEventArgs e)
{
var btn = (IButtonControl)sender;
switch (btn.CommandName)
{
case "Remove":
//delete it, use btn.CommandArgument to find id to remove
break;
}
}
If you still receive the ... Invalid postback or callback argument. Event validation is enabled using ... error, it most likely means that you are somewhere re-binding the repeater before the ibRemove_Click(...) event is raised.
I found the reason why this was happening in the answer to this question: Why ItemCommand doesn't fired on My Repeater
Turns out that for some reason the Page's DataBind function was invalidating the repeater's controls without passing through the repeater's OnItemDataBound function a second time.

How to hide a div using an ASP.NET Checkbox?

i'm trying to do this assigning an method to the event on the checkbox tag like this:
OnCheckedChanged="ShowDiv"
However, i'm not succesfull at all. How to do it? Here's my method:
public void ShowDiv(object sender, System.EventArgs e)
{
var div = FindControl("ListaEmOutrosDocumentos") as HtmlGenericControl;
var checkbox = FindControl("Principal") as CheckBox;
if(checkbox.Checked == true)
{
div.Style.Clear();
div.Style.Add("display","block");
}
else
{
div.Style.Clear();
div.Style.Add("display","none");
}
}
You might consider using an ASP.NET Panel control instead of a div. That will have a Visible property that you can set.
Quick way using server side code only
Aspx
<div id="myDiv" runat="server" style="height:200px;width:100px;background-color:Blue"></div>
<asp:CheckBox ID="chkShowHideDiv" runat="server" AutoPostBack="True"
oncheckedchanged="chkShowHideDiv_CheckedChanged" Text="Hide Div" />
Code behind
protected void chkShowHideDiv_CheckedChanged(object sender, EventArgs e)
{
myDiv.Visible = chkShowHideDiv.Checked ? false : true;
}
I did this and it worked.
<div id="ListaEmOutrosDocumentos" runat="server">
<asp:CheckBox runat="server" ID="Principal" AutoPostBack="True" OnCheckedChanged="ShowDiv"/>
</div>
Code Behind
public void ShowDiv(object sender, System.EventArgs e)
{
var div = ListaEmOutrosDocumentos as HtmlGenericControl;
var checkbox = sender as CheckBox;
if (checkbox.Checked == true)
{
div.Style.Clear();
div.Style.Add("display", "block");
}
else
{
div.Style.Clear();
div.Style.Add("display", "none");
}
}

Radio button doesn't get selected after a post back

I have an item template within repeater:
<ItemTemplate>
<li>
<input type="radio"
value="<%# GetAssetId((Guid) (Container.DataItem)) %>"
name="AssetId"
<%# SelectAsset((Guid) Container.DataItem) %> />
</li>
</ItemTemplate>
I have a method that compares ids and decides whether to check the radio button.
protected string SelectAsset(Guid uniqueId)
{
if (uniqueId == GetSomeId())
return "checked=\"checked\"";
return string.Empty;
}
SelectAsset gets hit, but it doesn't select a radio button on a post back, but it does work if I just refresh the page. What am I doing wrong here?
Answer here: How to display "selected radio button" after refresh? says that it's not possible to achieve, is this really the case?
Thank you
Update
It appears that view state isn't available for simple controls if they don't have a runat attribute. I have solved this by using a custom GroupRadioButton control. Thank you for your help.
I'd suggest using a RadioButtonList:
Page Code
<asp:RadioButtonList RepeatLayout="UnorderedList" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true" ID="RadioRepeater" runat="server" />
<asp:Label ID="SelectedRadioLabel" runat="server" />
Code Behind
if (!Page.IsPostBack)
{
/* example adds items manually
- you could iterate your datasource here as well */
this.RadioRepeater.Items.Add(new ListItem("Foo"));
this.RadioRepeater.Items.Add(new ListItem("Bar"));
this.RadioRepeater.Items.Add(new ListItem("Baz"));
this.RadioRepeater.SelectedIndex = this.RadioRepeater.Items.IndexOf(new ListItem("Bar"));
this.RadioRepeater.DataBind();
}
protected void IndexChanged(object sender, EventArgs e)
{
this.SelectedRadioLabel.Text = string.Format("Selected Item Text: {0}", this.RadioRepeater.SelectedItem.Text);
}
I assume you only need to select one item.
As described in the comments, it even works to access the SelectedItem in the Page_Loadevent handler:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// previous code omitted
}
else
{
string foo = this.RadioRepeater.SelectedItem.Text;
}
}
If you are creating all your controls dynamically at run-time (directly from code), then things are a little different. Here is the code that I used:
Page Code
<form id="form1" runat="server">
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl.AutoPostBack = true;
rbl.SelectedIndexChanged += rbl_SelectedIndexChanged;
rbl.Items.Add("All");
// generate your dynamic radio buttons here
for (int i = 0; i<5; i++)
{
rbl.Items.Add(string.Format("Dynamic{0}", i));
}
form1.Controls.Add(rbl);
if (!Page.IsPostBack)
{
rbl.SelectedValue = "All";
PopulateTextBox(rbl.SelectedValue);
}
}
void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList foo = (RadioButtonList)sender;
PopulateTextBox(foo.SelectedValue);
}
void PopulateTextBox(string selection)
{
TextBox box = new TextBox();
box.Text = selection;
form1.Controls.Add(box);
}

Two text boxes, either one or both are required

I have two textboxes on an asp.net webpage, either one or both are required to be filled in. Both cannot be left blank. How do I create a validator to do this in asp.net?
You'd need a CustomValidator to accomplish that.
Here is some code demonstrating basic usage. The custom validator text will show after IsValid is called in the submit callback and some text will be displayed from the Response.Write call.
ASPX
<asp:TextBox runat="server" ID="tb1" />
<asp:TextBox runat="server" ID="tb2" />
<asp:CustomValidator id="CustomValidator1" runat="server"
OnServerValidate="TextValidate"
Display="Dynamic"
ErrorMessage="One of the text boxes must have valid input.">
</asp:CustomValidator>
<asp:Button runat="server" ID="uxSubmit" Text="Submit" />
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
uxSubmit.Click += new EventHandler(uxSubmit_Click);
}
void uxSubmit_Click(object sender, EventArgs e)
{
Response.Write("Page is " + (Page.IsValid ? "" : "NOT ") + "Valid");
}
protected void TextValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (tb1.Text.Length > 0 || tb2.Text.Length > 0);
}
Try a CustomValidator.
You'll need to create a method that does the following to handle the ServerValidate event:
void ServerValidation (object source, ServerValidateEventArgs args)
{
args.IsValid = TextBox1.Text.Length > 0 || TextBox2.Text.Length > 0;
}
In addition to creating server-side validation, you can use the ClientValidationFunction property on the CustomValidator to provide client-side validation as well. That might look something like this:
function(sender, args) {
args.IsValid = document.getElementById('<%=TextBox1.ClientID%>').value != ''
|| document.getElementById('<%=TextBox2.ClientID%>').value != '';
}
Onclientclick of your button or whatever submits your page call a javascript function like this
function valtxtbox(){
if (document.getElementById('<%=TextBox1.ClientID%>').value== '' && document.getElementById('<%=TextBox2.ClientID%>').value== '')
{
alert('You must enter in data!');
return false;
}

Resources