I have a FormView that has elements that I want to make available to different users depending on the users access permissions. I have encased each of these inside of a panel and ID'd the panel so that I can call them out from the code behind potentially drilling into the FormView with FindControl but have been unsuccessful so far in getting it worked out.
I've never had much luck with FindControl and was wondering if some of you that are more knowledgeable about it could point me in the right direction here is an example of my code on the aspx and C# code behind that IS NOT working out!
If this were functioning then I would place conditionals to for valid groups under this protected void for this panel to allow these users access to this panel and likewise do the same for other panels where permission was applied.
In this way I would be presenting a custom FormView for each user group based on my ACL. But I just can't get through my head how to drill in with the FindControl properly.
ASPX SAMPLE:
<asp:FormView Width="100%" ID="ChangeFormFV" DefaultMode="Insert" runat="server" DataKeyNames="CAssetID" DataSourceID="UpdateSqlDataSource">
<InsertItemTemplate>
<asp:Panel runat="server" ID="ShortDescPnl" Visible="false">
</asp:Panel>
<asp:Panel runat="server" ID="LongDescPnl" Visible="false">
</asp:Panel>
<asp:Panel runat="server" ID="AddNotesPnl" Visible="false">
</asp:Panel>
<asp:Panel runat="server" ID="ManufacturerPnl" Visible="false">
</asp:Panel>
<asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" /> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
</ItemTemplate>
C# Codebehind (not working):
protected void ChangeFormFV_Databound(object sender, EventArgs e)
{
if (Session["SessionUType"].ToString() == "ITSec")
{
ChangeFormFV.Row.FindControl("ShortDescPnl.visiblity")="true";
}
}
Appreciate any help that can be offered, been searching for references and reading all I can but just not getting what I need from my results.
The FindControl method returns the actual control. Set the visibility on the control itself. Something like this:
Panel control = ChangeFormFV.Row.FindControl("ShortDescPnl") as Panel;
if (control != null)
control.Visible = true;
Related
ASP.NET radio button is not visible in my page. I am using it inside ASP.NET ListView. I tried putting the radio button inside ASP.NET update panel content template as well. Please tell me what has gone wrong?
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:ListView ID="FraudCheckList_listView" runat="server">
<ItemTemplate>
<p class="fraud-step-title"><%#Eval("question")%></p>
<div class="row">
<asp:RadioButton
runat="server"
class="radio-inline col"
GroupName="govt"
Text="Low Risk"
></asp:RadioButton>
<asp:RadioButton
runat="server"
GroupName='govt'
Text="Medium Risk"
></asp:RadioButton>
<asp:RadioButton
runat="server"
GroupName='govt'
Text="High Risk"
></asp:RadioButton>
</div>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
Since you haven't shared how your code looks like, i am just assuming certain things. Just see if you have done this or not.
void Button_Click(object sender, System.EventArgs e){RadioButton.Visible=false;}
If incase its true or something your radiobutton will not be visible. In your case it must be hidden. So, make the variable of visible as **false ** So you can just change your code to this and try it out.
This you have to do it in your main code. not in your html code.
Thank You!
I have an aspx page (say MyPage.aspx) where a part of it has the following structure -
<asp:DataList ... >
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
<asp:Table ID="table" runat="server">
<asp:TableRow ... >
<asp:TableCell ... >
<asp:ImageButton ID="btnToggle" OnClick="ToggleVisibility" ... >
</asp:TableCell>
...
</asp:TableRow>
</asp:Table>
<asp:DataGrid ... >
</asp:DataGrid>
<asp:Panel ID="panel" runat="server" ...>
<asp:Button ID="button1" runat="server" ...>
<asp:Button ID="button2" runat="server" ...>
</asp:Panel>
</ItemTemplate>
<AlternatingItemTemplate>
...
</AlternatingItemTemplate>
</asp:DataList>
What I am trying to do is that whenever btnToggle is clicked, it toggles visibility of panel. I'm getting the panel in ToggleVisibility() like this -
Dim panelToggle As Panel = sender.Parent.Parent.Parent.Parent.Controls(5)
In this function I'm able to change its Visible property, but its visibility doesn't change on the rendered HTML page (checking through browser).
I'm unable to figure out why is that. Kindly, help.
Thanks.
Add the OnItemCommand event to the DataList that handles the button click. You don't need to add the OnClick event to the button itself anymore.
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:Button ID="btnToggle" runat="server" Text="Button" />
<asp:Panel ID="panel" runat="server">
Panel content.
</asp:Panel>
</ItemTemplate>
</asp:DataList>
Then in code behind
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
//find the panel in the datalist item object and cast it back to a panel
Panel panel = e.Item.FindControl("panel") as Panel;
//you can now access it's properties
panel.Visible = false;
}
VB
Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)
'find the panel in the datalist item object and cast it back to a panel
Dim panel As Panel = CType(e.Item.FindControl("panel"),Panel)
'you can now access it's properties
panel.Visible = false
End Sub
After much searching and testing, it's time to ask for opinions.
A GridView inside an Update Panel with a file upload in an EditItemTemplate:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" >
<Columns>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" >
</asp:CommandField>
<asp:TemplateField HeaderText="Attachment" SortExpression="FileName">
<EditItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="btnAddAttachment" runat="server" Text="Upload File" CommandName="AddAttachment"
CommandArgument='<%# Bind("ID") %>' />
</EditItemTemplate>
<ItemTemplate>
<a id="ancLink" runat="server" href='<%# "~/Files/" + (DataBinder.Eval(Container.DataItem,"FileName")) %>'
target="_blank">
<asp:Label ID="lblAnchor" runat="server"></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Then for the button in the EditItemTemplate, add the RegisterPostBackControl:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState == DataControlRowState.Edit) || ((e.Row.RowState & DataControlRowState.Edit) > 0))
{
Button btnAddAttachment = (Button)e.Row.FindControl("btnAddAttachment");
AjaxControlToolkit.ToolkitScriptManager ToolkitScriptManager1 = (AjaxControlToolkit.ToolkitScriptManager)Master.FindControl("ToolkitScriptManager1");
ToolkitScriptManager1.RegisterPostBackControl(btnAddAttachment);
}
}
}
The problem is that the RegisterPostBackControl will not work the first time an attempt is made to upload a file. If a user edits the same row again, the second attempt works fine.
Most likely because the RegisterPostBackControl takes effect on the second post back.
Is there a way to have the button have a full postback the first time?
I know there is an easy way for a work around but this defeats the purpose of the UpdatePanel:
<Triggers>
<asp:PostBackTrigger ControlID="GridView1" />
</Triggers>
And since only Admins will have access to editing, setting the PostBackTrigger for the grid in the code behind for only admins is also an option, but once again, defeating the purpose of the Update Panel.
Any suggestions are welcome.
Depending on lots of factors, you could try the updatepannel option:
ChildrenAsTriggers="true"
It may work as a temporary workaround if you need to push something out now.
Based on your code you have the UpdatePanel using the default values so ChildrenAsTriggers is true and UpdateMode is Always, so you should get a full postback every time.
But I don't see you setting the Gridview's DataSourceID so it won't Databind unless you do so somewhere in the code behind. But you would have to be in edit mode initially in order to even find the control you're trying to register. So you need to register the control when you go into edit mode, try finding and registering the control in the gridview RowEditing event
I have a search form in an updatePanel which retrieves a list of users in a grid in the same UpdatePanel. The name of each user is a commandLink. I want to make the commandLinks as PostBackTriggers.
But when I do it I get an error at the pageLoad time that the controlId does not exist and its true because the grid of users does not render at the load time but through an ajax call.
Any ideas on how can I make the multiple command buttons in a grid retrieved through ajax call as post back triggers?
When adding the items to the grid, within the ItemDataBound event handler, you should register the postback for each specific control (the static identifiers in your HTML declarations are essentially placeholders - not all things repeated in the grid can actually have the same ID). You do this using the ScriptManager.RegisterAsyncPostBackControl method:
The RegisterAsyncPostBackControl method enables you to register Web
server controls as triggers so that they perform an asynchronous
postback instead of a synchronous postback. When the
ChildrenAsTriggers property of an UpdatePanel control is set to true
(which is the default), postback controls inside the UpdatePanel
control are automatically registered as asynchronous postback
controls.
As stated above, using ChildrenAsTriggers is a possibility, too, but this is commonly set to false for more stringent management.
I have found the solution. Here is the code on asp
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />
<asp:GridView ID="gvSearchResult" runat="server" OnRowCommand="gvSearchResult_RowCommand"
OnRowDataBound="gvSearchResult_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDetail" runat="server" CommandArgument='<%# Bind("CNIC") %>' CommandName="Detail">
<asp:Label ID="lblName" Text='<%# Bind("Employee_Name") %>' runat="server</asp:Label>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"Height="25px"Width="30%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Ihad to place OnRowDataBound="gvSearchResult_RowDataBound" on gridView and that function looks like below. So I had to register the iterative control in Scriptmanager as PostBackControl in RowDataBound event of GridView.
protected void gvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if ((e.Row.RowType == DataControlRowType.DataRow))
{
LinkButton lnkbtnDetail = (LinkButton)e.Row.FindControl("lnkbtnDetail");
ScriptManager.GetCurrent(this).RegisterPostBackControl(lnkbtnDetail);
}
}
catch (Exception ex)
{
}
}
OK, So Im fairly new to Web apps, been doing Windows app for a while in VB but Im switching to C# for web apps. Ok so I have a textbox and I have a SQLDataSource called SQLDataSource1. How in the world do I bind the source to the textbox so when it loads it will populate it?
Can someone give me some examples?
Ive tried the following,
Label1.Text = SqlDataSource1.SelectParameters.ToString();
But I get something other than the value of the SQLDataSource. I get this: System.Web.UI.WebControls.SqlDataSource
Thanks
I found an answer here on Forum.ASP.Net
Here is what has been said by Samu Zhang (Microsoft Online Community Support) :
It is hard to bind textbox to sqldatasource. We usually bind data controls such as FormView and GridView to sqldatasource and put one TextBox control in their Template . And you can invoke FindControl method of FormView to retrieve the textbox.
See my sample.
protected void Button1_Click(object sender, EventArgs e)
{
TextBox box = FormView1.FindControl("TextBox1") as TextBox;
}
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="countryid" DataSourceID="SqlDataSource1">
<ItemTemplate>
countryid:
<asp:Label ID="countryidLabel" runat="server" Text='<%# Eval("countryid") %>'></asp:Label><br />
countryname:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("countryname") %>'></asp:TextBox><br />
</ItemTemplate>
</asp:FormView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="retrieve value" />
</form>
One way is have your controls in a FormView or DetailsView, then set the DataSourceID of the FormView to the SqlDataSource and in your controls you bind them like so: asp:Label id=... Text='<%# DataBinder.Eval(Container.DataItem, "MyFieldName") %>' />, then on your OnInit of the page you can call MyFormView.DataBind();