ASP.NET is not calling C# code - c#

For some reason this following ASP code is not calling the c# method "checkLastNames" that is suppose to be evaluated in the 'Visible=' field below.
<asp:Button ID="btnZ"
runat="server"
Text="Z"
Height="20px"
Width="25px"
Font-Size="12px"
CommandArgument="Z"
OnClick="btnA_Click"
Visible='<%# checkLastNames("Z") %>' />
When I enter debug mode the method isn't even being called. Visible just defaults to true.
I've tried changing the method to return only false just to see if it would work but "Visible" is still defaulting to true.
protected bool checkLastNames(string s){
return false;
}

<asp:Button ID="btnZ"
runat="server"
Text="Z"
Height="20px"
Width="25px"
Font-Size="12px"
CommandArgument="Z"
OnClick="btnA_Click"
Visible='<%# checkLastNames("Z") %>' />
That # means it's only evaulated during a databind operation. So if you are not databinding the page explicitly (through calling DataBind()) then this won't show.
Visible='<%= checkLastNames("Z") %>' />
You might want to try the code above. Also I would probably put this in a static function (assuming it's functionality is encapsulated in there)

<%# is for databinding expressions, so this works only if the namingcontainer control of this Button is databound.
For example in Page_Load:
this.DataBind();
But why not using codebehind in the first place?
btnZ.Visible = checkLastNames("Z");

Try like this
<asp:Button ID="btnZ"
runat="server"
Text="Z"
Height="20px"
Width="25px"
Font-Size="12px"
CommandArgument="Z"
OnClick="btnA_Click"
Visible='<%= checkLastNames("Z") %>' />

Related

Pass parameter from Gridview column to Javascript

I need to pass an argument (which comes from my database) from a radgrid view column to my javascript (which opens a dialog box window). However, I can't put the "bind("Id")" as a parameter from where I call the javascript as href.
In simpler words, I am looking for a way to pass <% Bind("Id")%> to the javascript, OpenMyWindow, call instead of the hardcoded, 111, right now.
<telerik:GridTemplateColumn UniqueName="Meet" DataField="Subject" HeaderText="Meet">
<ItemTemplate>
<div style="text-align: center">
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("Subject") %>' href="javascript: OpenMyWindow(111);" Width="30%">
</asp:LinkButton>
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
When I try "OnClick" instead of "href", my popup dialog box closes instantly and doesn't stay opened.
Try using "OnClientClick" and return false from your javascript method to prevent post back.
Alternatively. You could use a method passing in the DataIten. Then output an anchor tag created anyway you like:
<%# formatOpener(Container.DataItem) %>
With code behind of:
protected string formatOpener(object item)
{
ObjectType myObj = (ObjectType)item;
return String.Format("<a href=\"javascript:OpenMyWindow({0});\" width=\"30%\"/>{1}</a>", myObj.ID, myObj.subject);
}
I think you don't need a LinkButton, you could achieve it with an asp:HyperLink (which renders as a a tag):
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("Subject") %>' NavigateUrl='<%# "javascript: OpenMyWindow(" + Eval("ID").ToString() + ");" %>'></asp:HyperLink>
Also, don't use Bind if you don't need it, for displaying purposes always use Eval.
User HyperLink control instead and try setting the NavigateUrl property of the HyperLink this way:
<asp:HyperLink ID="hlLink" runat="server" Text='<%# Bind("Subject") %>' NavigateUrl='<%#Eval("Id", "javascript: OpenMyWindow({0});")%>'>
hope it helps./.
I finally figured out and solved this issue. Actually, somebody told me on the other post that, CommandArgument is completely a server-side property and doesn't render any html attribute. So I can't change any button's attribute and fire click on it. I finally made "Id" come through the code behind and made it work.
aspx code
<telerik:GridButtonColumn UniqueName="Subject" DataTextField="Subject" HeaderText="Meeting">
<HeaderStyle Width="30%" />
<ItemStyle HorizontalAlign="Center" />
</telerik:GridButtonColumn>
Code Behind
var subjectLink = meetingRow["Subject"].Controls[0] as LinkButton;
subjectLink.Attributes.Add("onClick", "javascript: return OpenMyWindow('" + meetingId + "')");

WebForms Model Binding: Automatically map updated values to Item

I'm currently working on a little dummy application to get my head around the new (awesome) model binding in WebForms. I've succesfully listed data both in a ListView and a FormView, however I'm stuck on updating the data.
I have a very simple FormView control:
<asp:FormView runat="server" ID="formViewOrderDetail"
ItemType="ModelBindingDummy.Models.Order" DataKeyNames="Id"
SelectMethod="GetOrder" UpdateMethod="UpdateOrder">
<ItemTemplate>
<p>Supplier Order Number: <%#: Item.SupplierOrderNumber %></p>
<asp:LinkButton Text="Edit" runat="server" ID="linkEdit"
CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<p>Supplier Order Number:
<asp:TextBox runat="server" ID="SupplierOrderNumber"
Text='<%#: Item.SupplierOrderNumber %>' />
</p>
<asp:LinkButton Text="Cancel" runat="server" CommandName="Cancel" />
<asp:LinkButton Text="Save" runat="server" CommandName="Update" />
</EditItemTemplate>
</asp:FormView>
My UpdateOrder method looks like this:
public void UpdateOrder(int id)
{
ModelBindingDummy.Models.Order item = GetOrder(id);
if (item == null)
{
ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
return;
}
TryUpdateModel(item);
//All data in the app is in-memory, so no need to actually update
//the item as it is only a reference
}
Now my problem is that the TryUpdateModel doesn't work. It simply ignores the value in the TextBox SupplierOrderNumber
I've tried adding a parameter to the update method, [Control] string supplierOrderNumber, and while that works, I'd really like to see a way to automatically map that value.
I've also seen solutions using the <asp:DynamicEntity /> control, but that seems somewhat overkill.
So my question is: Is it possible to get the TryUpdateModel method to map the TextBox value, or do I need to go all the way with the DynamicEntities control, or are there any other fancy solutions?
Thanks!
You need to use BindItem.SupplierOrderNumber instead of Item.SupplierOrderNumber.
You need to use DataItem.SupplierOrderNumber instead of Item.SupplierOrderNumber
There is another question like this out there with a better explanation but i can't find it.

Visibility of an ASP.Net button which depends if a file exists in database

I have a gridview which has a few rows (each with a unique rowId), and each line has a FileUpload control, now everything works okay with FileUpload.
(my uploaded file database image can be seen below)
I have the download button, which also works okay, however I want to make this button invisible if no file exists for the corresponding row.
Nothing proper comes to my mind.
My button and FileUpload control:
<asp:TemplateField HeaderText="BatchList">
<EditItemTemplate>
<asp:ImageButton ID="ibt_Download" runat="server" src="Images/Download.png" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>' ></asp:ImageButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UploadBatchList">
<HeaderTemplate>
<asp:Label ID="lbl_Header" ToolTip="Upload Batch List" runat="server" Text="UBL"></asp:Label>
</HeaderTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu_UploadBatchList" runat="server" />
<asp:Button ID="btn_Upload" runat="server" Text="Upload" OnClick="btn_Upload_Click" />
</EditItemTemplate>
</asp:TemplateField>
This is how it looks on my gridView
When gridview is first created the green dots must not be visible if a file has been uploaded before.
My file database:
You can check some property of the data item (DocName in your case) if it contains a value (it might not work when copy-pasted, I'm a little bit improvising):
<asp:Button ID="btn_Upload" runat="server"
Text="Upload"
Visible='<% DataBinder.Eval(Container.DataItem, "DocName") == null %>
OnClick="btn_Upload_Click" />
Or you can create a function that will evaluate the visibility. See Mastering ASP.NET DataBinding for more.

Textbox control in ASP.NET not to be part of a formview

I have to add a textbox inside a form that is composed of a lot of textboxes, and one button at the end. It has a datasource, all data is loaded in formload, and the button updates the values of the textboxes. The thing is: this particular textbox won't be in the datasource, I want to get this from the web.config, and I've already managed to change the web.config in other page, but in this case, the textbox ID won't appear in intellisense in the code-behind of the page itself, so I figured it's unaccessible to anything besides the pure binding of the form.
<asp:FormView>
<EditItemTemplate>
<asp:TextBox ID="id" runat="server" Text='<%# bind("field") %>'/>
<asp:TextBox ID="id2" runat="server" Text='<%# bind("field2") %>'/>
<asp:TextBox ID="id3" runat="server" Text='<%# bind("field3") %>'/>
<asp:TextBox ID="THIS_ONE" runat="server"></asp:TextBox> <!--HERE-->
<asp:Button ID="UpdateButton" runat="server" SkinID="UpdateButton" CommandName="Update"/>
</EditItemTemplate>
</asp:FormView>
Above, I have an unaccessible textbox.
<asp:FormView>
<EditItemTemplate>
<asp:TextBox ID="id" runat="server" Text='<%# bind("field") %>'/>
<asp:TextBox ID="id2" runat="server" Text='<%# bind("field2") %>'/>
<asp:TextBox ID="id3" runat="server" Text='<%# bind("field3") %>'/>
<asp:Button ID="UpdateButton" runat="server" SkinID="UpdateButton" CommandName="Update"/>
</EditItemTemplate>
</asp:FormView>
<asp:TextBox ID="THIS_ONE" runat="server"></asp:TextBox><!--HERE-->
Above, I have an accessible textbox, but BELOW the update button.
I've already tried closing EditItemTemplate before the textbox and re-opening it afterwards. Doesn't work.
I could of course put it below the button, below where the form ends, then they wouldn't be part of the form, and that would work, but what if I want the textboxes ABOVE the button ? I want accessible unbinded textboxes inside an ASP.NET formview. Is that possible ?
ps.: I know the implications of messing with web.config in run-time and I know that this doesn't seem well planned, but I didn't say some details that don't matter for this question.
If your issue trying to access the textbox? You can use FormView1.FindControl() to obtain a reference to the control within the formview...

Why doesn't User.IsInRole work in this context?

...I want to Show the 'delete' button when user is an admin, and show the 'add item' button when user is a contributor:
<!-- More code above -->
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton CSSClass="TableRightLink" ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Visible=<%# User.IsInRole(#"DOMAIN\CMDB_ADMIN") %>
Text="Delete"
OnClientClick="return confirm('Are you certain you want to delete this item?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle VerticalAlign="Top" />
<HeaderStyle ForeColor="White" CssClass="TableHeader" BackColor="SteelBlue" />
</asp:GridView>
<asp:table width="100%" runat="server" CSSclass="PromptTable" Visible=<%# User.IsInRole(#"DOMAIN\CMDB_CONTRIBUTE") %> >
<asp:tablerow><asp:tablecell HorizontalAlign=Center>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="AddConfigItem.aspx" ForeColor="LightCyan">Add Item</asp:HyperLink>
</asp:tablecell></asp:tablerow></asp:table>
The Delete button 'visible' attribute works fine. But, the "add item' hyperlink doesn't. It always shows.
View-source tells me that %# User.IsInRole(#"DOMAIN\CMDB_CONTRIBUTE") %> isn't evaluating to anything. Any idea why this is?
Try setting it in code behind, instead of in mark up, in Page_Load. Assuming the id is promptTable (it wasn't given in your example), just add:
promptTable.Visible = User.IsInRole(#"DOMAIN\CMDB_CONTRIBUTE");
Presumably this needs to be done regardless of whether it is a postback or not.
FWIW, #Keltex is right about the control not being databound so <%# %> won't work. Unfortunately, the <%= %> syntax won't either because it always returns a string and you need a boolean value there. I couldn't find any other syntax that would work in this case. You could probably do this by turing off display using javascript, but I suspect that you don't want the table to be rendered to the page if not in the correct group (as opposed to just being hidden or removed from the DOM once on the client). Doing it in the code behind, I think is the right way to go about it.
Try:
Visible='<%= User.IsInRole(#"DOMAIN\CMDB_CONTRIBUTE") %>'
The asp:table doesn't appear to be databound.

Categories