div from gridview - onclick width changing - c#

can somebody tell me following code in asp.net c# codebehind.
If i click and select any row in gridview ---> change CSS-DIV width, which runat server.
And where do i have to paste it? i tryed few things, but it doesnt matter where i code it, the width changes on page_load instead of "on gridview click"
Generally i need it for that scheme http://ipic.su/img/img7/fs/ChangeWidth.1412455799.jpg
Thanks for any advises!

Try this:
if (!Page.IsPostBack)
{
dvtimeSheet.Visible = false;
//hide your details view
}
Add On OnSelectedIndexChanging Event to GridView
OnSelectedIndexChanging="gvTimeSheet_OnSelectedIndexChanged"
and Use In Code Behind
protected void gvTimeSheet_OnSelectedIndexChanged(Object sender, EventArgs e)
{
gvTimeSheet.Visible = false;
dvtimeSheet.Visible = true;
dvtimeSheet.Style.Add("left", "120px");
dvtimeSheet.Style.Add("height", "260px");
dvtimeSheet.Style.Add("width", "120px");
dvtimeSheet.Style.Add("position", "absolute");
//same you can do with your div(making true/false or adding style)
}
Note: Use Update Panels with EnableEventValidation="false" in Page Directives
and Use Firebug for mozilla to adding style and alignments
EDIT:

Related

My formview wont change modes C#

I have a FormView that has an ItemTemplate, EditItemTemplate and InsertItemTemplate. I recently changed from VB to C# and my VB version worked perfectly. From what I can tell the only difference in the two versions is the datasource. My VB FormView used SqlDataSource, my C# is using EntityFramework. The DefaultMode is set to ReadOnly and when I click the Edit Command button I get a post back but the FormView doesn't change. If I click the button a second time I get the following error.
Line: 6
Error: Sys.WebForms.PageRequestManagerServerErrorException: Failed to load
viewstate. The control tree into which viewstate is being loaded must
match the control tree that was used to save viewstate during the previous
request. For example, when adding controls dynamically, the controls added
during a post-back must match the type and position of the controls added
during the initial request.
I have
protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
{
}
because I was getting an error saying that I didn't have the ModeChanging event handled.
Here is my EditButton_Click()
protected void EditButton_Click(object sender, EventArgs e)
{ //I know that the FormView is supposed to automatically ChangeModes when the editbutton
//is clicked. However, I have tried with and without the below statement.
FormView1.ChangeMode(FormViewMode.Edit);
}
I fill my FormView with data using EntityFramework
//This is in my pages codeBehind
protected void ViewPartnerBtn_Click(object sender, EventArgs e)
{
List<FTP_GET_TESTING> dataList = ftpGetData.GetFTPSetup(FTPLookupDDL.SelectedValue);
FormView1.DataSource = ftpGetData.GetFTPSetup(FTPLookupDDL.SelectedValue);
FormView1.DataBind();
FormView1.ChangeMode(FormViewMode.ReadOnly);
}
//This is in my dataAccess class
public List<FTP_GET_TESTING> GetFTPSetup(String ftpLookupId)
{
using (Entities myEntities = new Entities())
{
var allFields = (from ftpGet in myEntities.FTP_GET_TESTING
where ftpGet.FTP_LOOKUP_ID == ftpLookupId
select ftpGet).ToList();
return allFields;
}
}
More info
My formview is contained within an update panel and I have tried a scriptManager in my master page, I have tried adding a scriptManagerProxy in my contentPage and I have also tried removing the scriptManager from the master and adding it directly to the contentPage.

Overriding the Telerik RadGrid AddNewRecordButton functionality to redirect the user to a new page rather than add a new insert record row to the grid

Previously when my RadGrid was not a batch edit grid I was able to use the grid's AddNewRecord button to redirect the user to another page with the following code:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "InitInsert")
{
Response.Redirect(redirectUrl + "?ProductID=" + this.ProductId);
}
}
After I made my grid a batch edit grid the Add New Button doesn't go into the ItemCommand event anymore and instead tries adding an inline insert record row to the grid. Is there anyway I can still use this button and override its functionality to still redirect the user?
So I've tested this and confirmed what I suspected in the comments. When EditMode="Batch", the "Add New Record" button, along with others, no longer cause a postback. You can override this by removing the JavaScript of the OnClientClick in the RadGrid1_ItemCreated like so:
Add this to your RadGrid1 attributes:
OnItemCreated="RadGrid1_ItemCreated"
Code behind (note: there is actually a Button AND a LinkButton):
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item.ItemType == Telerik.Web.UI.GridItemType.CommandItem) {
//This is the icon with the plus (+) sign unless you've changed the icon
Button iconButton = e.Item.FindControl("AddNewRecordButton");
if (iconButton != null) {
iconButton.OnClientClick = "";
}
//This is the words "Add New Record" or whatever you've called it
LinkButton wordButton = e.Item.FindControl("InitInsertButton");
if (wordButton != null) {
wordButton.OnClientClick = "";
}
}
}
This should allow the postback to happen and the code you posted should be able to run.

Hide a Panel when GridView is empty

I've a GridView inside a Panel that I want to hide when the child is empty because at the moment remains a fieldset with the legend text and nothing inside.
I've already tried to put something like Panel.Visible = GridView.Rows.Count > 0 in the Page_Load event but it doesn't works well.
How can I obtain the result that I need?
Thank you
Some more details:
If on first load the database table is empty I don't see the fieldset, when I add a row the Panel with the GridView doesn't appears; if on first load I have a row I can see the Panel with the GridView, when I delete the unique row anything disapears but never come back even if I insert a new row. I think that the Page_Load is not the right event.
try this..
Panel.Visible = (GridView.Rows.Count > 0?false:true);
Please try following steps:
Check for Panel.Visible = GridView.Rows.Count > 0 in a late page event such as Page_PreRender
Add OnRowDeleted event in GridView and check for Panel.Visible = GridView.Rows.Count > 0 if the row deleting is a last row. Also rebind the GridView as GridView.DataBind()
Add OnRowCreated event in GridView and repeat the same process as you did in case of row deletion.
Code:
protected void Page_PreRender(object sender, EventArgs e)
{
Panel.Visible = GridView.Rows.Count > 0;
}
protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
{
GridView.DataBind();
Panel.Visible = GridView.Rows.Count > 0;
}
protected void GridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
GridView.DataBind();
Panel.Visible = GridView.Rows.Count > 0;
}
I hope this will solve your issue.
In the end I found my problem...
I've put the FormView for the insert and the GridView in different UpdatePanels, so unified all in the same UpdatePanel and used this code:
Panel.Visible = GridView.Rows.Count > 0;
in the GridView DataBound event and it worked.
Thank you everyone.
If you simply want to make your panel visible/invisible based on the Gridview Rows, then its as simple as this :
if(GridView.Rows.Count > 0)
PanelId.Visible = true;
else
PanelId.Visible = false;
But make sure you do this code after you have called the Gridview binding function.
Hope this helps.

How do I use the hyperlink field in gridview, a new pop-up, to link back to my main page

I have created a page with a button that opens up a new page, a pop-up if you will.
btnToolbarSearch.Attributes.Add("onclick", "window.open('DagbokSearch.aspx','','height=600,width=600');return false");
On this new page that opens up I have a gridview where you get the below info. (You do a search on the "from date" to the "to date" and get the records in between.)
The first column where it says "Gå till" is a link
<asp:HyperLinkField DataNavigateUrlFields="Foretag"
DataNavigateUrlFormatString="userProfile.aspx?ID={0}"
Text="Gå till" />
I would like this link to get me back to the previous page and open up the object with the corresponding id, I'm not sure how to accomplish this. Maybe there is a better way then the one I'm using but I'm still learning.
You should be able to use the window.opener property to get a reference to the parent window. You can then set it's URL to the selected link, and close the popup window.
Something like this should do the trick:
// Place this near your closing </body> tag
// NB Uses jQuery and event delegation
$(function() {
$('table').on('click', 'tr > td:first > a', function(event) {
if (window.opener) {
event.preventDefault();
window.opener.location.href = this.href;
window.close();
}
});
});
You can set its NavigateUrl property in the Rowdatabound event of the gridview. Like;
protected void gvDogBok_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((HyperLink)e.Row.Controls[1].Controls[1]).NavigateUrl = "~/userProfile.aspx?ID="+((YourType)e.Row.DataItem).ID+"";
}
}
I'm not sure if what you are asking can be done. I would advice you to use a floating div popup instead. This way you dont have to leave the current page and go to a new tab. This should solve your problem and does avoid problems with popup blockers.
Here are some examples: http://www.javascripttoolbox.com/lib/popup/example.php
Use this one
protected void gvDogBok_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((HyperLink)e.Row.Controls[1].Controls[1]).Attribute.Add("onclick", "window.opener.location =userProfile.aspx?ID="+ ((YourType)e.Row.DataItem).ID+"; window.close();";
}
}
You can set JavaScript:window.location.replace(url); to the clientside onclick of the HyperLink. window.location.replace(url) will reload the page.
btnToolbarSearch.Attributes.Add("onclick", "var windowHandle = window.open('DagbokSearch.aspx','','height=600,width=600');return false");
and on hyperlink cleint side onclick
hyperLink.Attributes.Add("onclick", "windowHandle.close();window.location.replace(url);return false");

Edit, Delete and Insert in a programmatically created GridView

So I'm facing a problem. In fact i have created a databound GridView programmatically. The problem is I want to be interactive (update , delete, insert and all that drill) but only using c# since i have created it programmatically.
Here is the code that'ive created with the GridView
GridView grid = new GridView();
//CSS
grid.CssClass = "grid";
grid.ForeColor = System.Drawing.Color.Gray;
grid.ShowFooter = true;
grid.AlternatingRowStyle.CssClass = "gridAltRow";
grid.RowStyle.CssClass = "gridRow";
grid.FooterStyle.CssClass = "gridFooterRow";
grid.DataKeyNames = new string[] { "ID" };
grid.RowCommand
//End Css
grid.AutoGenerateEditButton = true;
grid.DataSource = element.GetElementByRubric(testrub.ID);
panel.Controls.Add(grid);
grid.DataBind();
TabContainer1.Tabs.Add(panel);
as you can see i added an edit button but it does nothing of course. Does anyone have any suggestions ??
thx
You have to write the function for updating the database in onrowupdating event of the gridview to make the edit link work.
Why are you generating a grid in code? This is highly unusual.
It may or may not be possible but why are you avoiding just writing the grid directly to the ASPX file?
You need to add a handler for the OnRowEditing event. Something similar to
grid.RowEditing+=new GridViewEditEventHandler(grid_RowEditing_RowEditing);
to attach the event handler and then an event handler like below. In this case the sender will be the dynamically created GridView.
protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
{
((GridView)sender).EditIndex = e.NewEditIndex;
// Then call your databinding method here to update the grid.
}
This would also be the same method you would use to add handlers for the delete and insert events also.

Categories