I have the below Image Button which resides in a Repeater:
<asp:ImageButton PostBackUrl='<%# DataBinder.Eval(Container.DataItem, "VesselId", "getattachment.do?VesselId={0}") %>' CausesValidation="false" ID="insurancestatus" runat="server"/>
So every time the user clicked on the button they were shown their attachment.
The requirement has since changed, and the attachment must open on a new page so I added the following to the Repeaters ItemDataBound event:
ImageButton imagebuttonInsurance = (ImageButton)e.Item.FindControl("insuranceStatus");
imagebuttonInsurance.OnClientClick = String.Format("aspnetForm.target = '_blank'; return true()");
Now when I click on the image it does open another page but it just reloads the previous page on the new page. I tried removing the PostBackUrl and wiring up the following click event:
protected void insurancestatus_Click(object sender, ImageClickEventArgs e)
{
Vessel vessel = (Vessel)e.Item.DataItem;
Response.Redirect(String.Format("~/Secure/getattachment.do?VesselId={0}", vessel.VesselId));
}
But I couldn't get a handle on the item so cannot use the VesselId for the link, where am I going wrong here?
Use the second method, but use the command argument property like:
<asp:imagebutton runat=server...... commandargument='<%# Eval("vesselid") %>' />
then:
protected void insurancestatus_Click(object sender, ImageClickEventArgs e)
{
string vesselId = ((ImageButton)sender).CommandArgument;
Response.Redirect(String.Format("~/Secure/getattachment.do?VesselId={0}", vesselId));
}
Related
I am showing my data in data list using grid view each grid view includes a button click how how can I store a particular button click value in a session and redirect the value to next page
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Button2")
{
Response.Redirect("movers_packers_profile.aspx?sp_id=" + e.CommandArgument.ToString());
}
}
Please check whether your button is set like this
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Button2" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "sp_id")%>' />
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 FormView with data(DataSource,DataBind) that I fill with value='<%# Eval("Name") %>' , but after I'm changing the text in TextBox and press update button I see the same value that before, I cant see new value that I have typed.
What I am missing here?
my html
<asp:FormView ID="MainFormTemplate" runat="server">
<ItemTemplate>
<li class="li_result" runat="server">
<div class="col-3">
<input id="txt_Name" runat="server" value='<%# Eval("Name") %>'>
</div>
</li>
</ItemTemplate>
</asp:FormView>
<asp:Button id="btn_Update" runat="server" OnClick="btn_Update_Click" Text="Update" />
Server side
protected void Page_Load(object sender, EventArgs e)
{
using (DB_MikaDataContext data = new DB_MikaDataContext())
{
MainFormTemplate.DataSource = data.File_Projects.Where(x => x.Num_Tik.Equals("12")).ToList();
MainFormTemplate.DataBind();
}
}
public void btn_Update_Click(object sender, EventArgs e)
{
//using System.Web.UI.HtmlControls
HtmlInputText twt = (HtmlInputText)MainFormTemplate.FindControl("txt_Name");
string text = twt.Value;//i see old value ,not new one that i typed in text box
}
In every postback, you are always getting the old value from your database. The solution is check if the page is being rendered for the first time (!IsPostBack) then set your MainFormTemplate's DataSource else if is being loaded in response to a postback (IsPostBack) get the txt_Name's value like this:
HtmlInputText twt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (DB_MikaDataContext data = new DB_MikaDataContext())
{
MainFormTemplate.DataSource = data.File_Projects.Where(x => x.Num_Tik.Equals("12")).ToList();
MainFormTemplate.DataBind();
}
}
else
{
twt = MainFormTemplate.FindControl("txt_Name") as HtmlInputText;
}
}
protected void btn_Update_OnClick(object sender, EventArgs e)
{
string text = twt.Value; // You will get the new value
}
with Page_Load executing every postback, you are always writing value from database (?), and value sent from browser is lost (although still exist in Page.Request.Form member).
In ASP.NET, When a page is submitted, the Page_Load event runs before the button click event. So, the textbox value gets repopulated with its original value before the click event looks at that value.
If this is the situation, then you can wrap the code that assigns the value to the textbox in an if block like this:
if (!IsPostBack)
{
HtmlInputText twt = (HtmlInputText)MainFormTemplate.FindControl("txt_Name");
string text = twt.Value;
}
Hope this helps you.
So I need to add some link buttons to each cell of a asp:calendar control dynamically based on data in the database. I wondering what the best way to do this is so that the the link buttons will be wired up to their events on postbacks (as to my knowledge creating the link buttons on the Day Render event and adding them there will occur after the LinkButton events would have fired).
Does anyone have a nice way to handle this?
EDIT: This is a hack but it works, you will have to get crafty with the event naming...etc
Markup:
<asp:Calendar id="calendar1"
OnDayRender="DayRender"
runat="server">
<asp:LinkButton ID="LinkButton1" style="display:none;" runat="server"
onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
Code-Behind:
protected void DayRender(Object source, DayRenderEventArgs e)
{
LinkButton lb = new LinkButton();
lb.ID = "LinkButton1";
//set all your props
lb.Attributes.Add("href",
"javascript:__doPostBack('" + Calendar1.UniqueID + "$" + lb.ClientID +"','')");
e.Cell.Controls.Add(lb);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
//do something
}
I have this linkbutton within a datalist and I'm trying to get access to the datalist on the pageload so i can set the linkbutton to be enabled or not based on the user's role.
<asp:DataList id="dlRecommendations" runat="server" DataKeyField="Key" Width="900">
<ItemTemplate>
<asp:LinkButton id="lnkEdit" Text="Edit" Runat="server" CommandName="Edit">
</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
Within the page load I want to be able to access the linkbutton to enable or disable it based on the user's role.
private void Page_Load(object sender, System.EventArgs e) {
//perhaps something like this:
lnkEdit.Enabled = false;
....
}
I think you will be populating the datalist the first time page is loaded. So just wireup ItemDataBound, find link and disable it.
void dlRecommendations_ItemDataBound(object sender, DataListItemEventArgs e)
{
var link = e.Item.FindControl("lnkEdit") as LinkButton;
if (link != null)
{
link.Enabled = UserHasRight;//if user has right then enabled else disabled
}
}
DataList is a databound control - it builds rows only when data is supplied.
To access link inside row use ItemDataBound event and access e.Item.FindControl("linkId");